As I said I got the level placer script working, so I can create, destroy, and switch levels on the fly without switching scenes. But I can't seem to work out how to make a player script that will check for the move variable to be true, and then call a move function that will see what block will the player run into if he goes that direction and then move the ball there. I could use raycasting, but I have it set up so all objects on the map are in a 3d objects array. What would be the best way to do this. Here is the level generation code if it helps. BTW all the following code is made from scratch so it may not be the most optimized code in the world.
Code: Select all
using UnityEngine;
using System.Collections;
public class LevelGeneration : MonoBehaviour {
public int currentLevel = 1;
int levelSizeZ = 20;
int levelSizeX = 20;
public bool createLevel = false;
public bool destroyLevel = false;
public GameObject wallPrefab;
public GameObject playerPrefab;
Object createObject;
Object[,] objectsInLevel;
int[,] objectsInLevelReference;
int[,] level1Generation = new int[20,20]
{{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,1,1,1,0,0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,0,0,0,0,0,0,0}};
// Use this for initialization
void Start () {
createLevel = true;
objectsInLevel = new GameObject[levelSizeX, levelSizeZ];
objectsInLevelReference = new int[levelSizeX, levelSizeZ];
}
// Update is called once per frame
void Update () {
if (createLevel == true) {
switch (currentLevel) {
case 1:
DestroyLevel ();
GenerateLevel (level1Generation);
break;
case 2:
DestroyLevel ();
GenerateLevel(level2Generation);
break;
}
createLevel = false;
}
if (destroyLevel == true)
{
DestroyLevel ();
destroyLevel = false;
}
}
void GenerateLevel(int [,]generationMap){
for (int gpz = 0; gpz < levelSizeZ; gpz++)
{
for(int gpx = 0;gpx < levelSizeX; gpx++)
{
CreateGridObject(gpx, gpz, generationMap);
}
}
}
//void GenerateLevel2(){
//for (int gpz = 0; gpz < levelSizeZ; gpz++)
//{
//for(int gpx = 0;gpx < levelSizeX; gpx++)
//{
// CreateGridObject(gpx, gpz, level2Generation);
// }
//}
//}
void CreateGridObject(int x,int z,int[,] levelGenerationMap)
{
switch (levelGenerationMap [x,z]) {
case 1:
//objectsInLevel[x,z] = wallPrefab;
createObject = Instantiate(wallPrefab, new Vector3(x, 0, z), Quaternion.identity);
objectsInLevel[x,z] = createObject;
objectsInLevelReference[x,z] = 1;
//Instantiate(objectsInLevel[x,z], new Vector3(x, 0, z), Quaternion.identity);
break;
case 2:
//objectsInLevel[x,z] = wallPrefab;
createObject = Instantiate(playerPrefab, new Vector3(x, 0, z), Quaternion.identity);
objectsInLevel[x,z] = createObject;
objectsInLevelReference[x,z] = 2;
//Instantiate(objectsInLevel[x,z], new Vector3(x, 0, z), Quaternion.identity);
break;
}
}
void DestroyLevel(){
for (int gpz = 0; gpz < levelSizeZ; gpz++)
{
for(int gpx = 0;gpx < levelSizeX; gpx++)
{
DestroyGridObject(gpx, gpz);
}
}
}
void DestroyGridObject(int x,int z)
{
switch(objectsInLevelReference[x,z]) {
case 1:
//objectsInLevel[x,z] = wallPrefab;
Destroy (objectsInLevel[x,z]);
objectsInLevelReference[x,z] = 0;
//Instantiate(objectsInLevel[x,z], new Vector3(x, 0, z), Quaternion.identity);
break;
case 2:
Destroy (objectsInLevel[x,z]);
objectsInLevelReference[x,z] = 0;
break;
}
}
}