Page 1 of 1

I Am Working On A Small Puzzle Game In Unity 3d. But I Need Help.

Posted: June 8th, 2015, 7:01 pm
by RetroBandit
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.

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,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}};

    int[,] level2Generation = new int[20,20]
    {{1,1,1,1,1,1,1,1,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},
    {0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
    {0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
    {0,0,1,1,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,1,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,1,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,1,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,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;

        }
    }
}

Posted: June 9th, 2015, 6:48 pm
by bionicnacho
iam12not3man wrote:BI could use raycasting, but I have it set up so all objects on the map are in a 3d objects array.
Honestly, raycasting would be the best way to do this imo.

If they're game objects, why not give the different types of blocks different tags and simply assign them while instantiating. This will allow you to just check the tag that's on the object with the raycast.

Note, I haven't fired up Unity in a long, long time. Maybe it's a layer you have to use instead of a tag, I don't remember.

You could also do a similar thing with the name of the object. Set the name, and check it when raycasting.

Posted: June 9th, 2015, 7:27 pm
by RetroBandit
There is only one wall gameObject, there is a grid of wall objects, not wall GameObjects

Posted: June 9th, 2015, 8:16 pm
by bionicnacho
Not entirely sure what you mean

Posted: June 10th, 2015, 4:07 pm
by RetroBandit
each wall dosn't have a unique name or tag. The only unique thing about them is there place in the object array. I would I find out which object in the object array I hit?

Posted: June 12th, 2015, 6:30 pm
by bionicnacho
Do you have a way of knowing what object is in a certain coordinate?