I Am Working On A Small Puzzle Game In Unity 3d. But I Need Help.
Posted: June 8th, 2015, 7:01 pm
Basically the game consists of you having to move player pieces ice style around the board to solve puzzles. The catch is that because the way I have the generation script set up. I can switch out full or portions of levels in a flash. It could be a multi floor ice puzzle, or something where a quarter of the level changes when one of the players are on a green tile and so on. And it is all grid based so I can change every individual thing about the level in game.
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.
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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,0,0,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,2,1,0,0,0,0,0,0,0},
{0,0,0,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,0,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,0,0,0,0,0,0,0,0},
{0,0,0,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,2},
{0,0,0,0,0,0,0,0,0,0,0,0,0,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;
}
}
}