Search Unity

Grid Question (Part 2)

Discussion in 'Getting Started' started by DocJ, Feb 22, 2017.

  1. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    Hi,
    I had some really good advice by JoeStrout but I can't seem to find what I'm looking for to follow through on it. If anyone can push me further in the right direction of where to look, I would appreciate it very much.

    JoeStrout said:
    Yes, you could have a script on some sort of game manager object, with a public array of GameObject (or whatever other more specific type you like). Then you could drag the objects in the scene, into the array on this manager object.

    What I'm trying to accomplish is somewhat like the program Tiled, but with 3D objects. I am trying to create a "game board" with the layout maybe 4 tiles wide x 9 tiles tall, with about 9 different 3D tiles each with their own scripted features/events.

    My difficulty lies in the fact that everything I find for reference deals with sprites instead of 3D objects, or the layout is randomly generated. I would like to be able to design my own layout and use grid ( [x,y] ) coordinates to determine which tile the character is on after a dice roll. I have generated a public array with all the prefab game objects in it, but I'm not sure where/how to generate where I want them.

    I have searched high and low and really wanted to be able to run with the advice JoeStrout gave me, but I'm stuck. If someone could please push me in the right direction. Sorry if I am just overlooking something obvious, this is my first time working with arrays.
    Thank you,
    Doc
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    This doesn't matter. There is almost no difference between sprites and 3D objects in Unity. They're all GameObjects with a Transform, and you deal with them in exactly the same way.

    Yes, that's a bit harder than what you want to do, if I understand you correctly. Randomly generating a layout requires code. Arranging objects in a specific layout requires no code.

    Those are two separate things. Always try to separate your problems and deal with them one at a time.

    OK, I think I see the problem. The array should not have the prefabs in it. It should have the instances in it. Clear out your array. Then do this:
    1. Grab (click and hold with the mouse) a prefab from wherever it lives in your Project tab.
    2. Drag it to the Hierarchy tab (and release the mouse). Or you can drag it to the Scene tab. Either works.
    3. Find the newly created object in your scene.
    4. Position it where you want it using the Move, Rotate, and Scale tools as described in the manual.
    5. Drag the instance from the Hierarchy tab into your array.
    6. Go back to step 1. Repeat until done.

    HTH,
    - Joe
     
    DocJ likes this.
  3. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    I'm not sure I'm interpreting your problem correctly, but here's what I'm getting from your question.

    Create a 2D integer array and call it 'terrainMap', The map will contain a value within a range of your 3D prefab count.

    Create an array of GameObjects and add your terrain prefabs to it.

    Populate the terrainMap with values.

    loop through a double 'for' loop read the terrainMap value and instantiate the prefab that corresponds to the terrainMap value and position by using a multiple of your for loop indexes.

    organizing your prefabs to correspond to the map; 0 = water, 1 = dirt, 2= grass, 3 = sand, 4 = hill, 5 = trees, 6 = rocks. You may want to generate some rules to prevent or force some terrain parts to be or not be next to each other. This assumes your terrain prefabs are the same size (x, y, & z) and heightmaps should somewhat match each adjacent object..

    Your terrain maps can be saved and loaded for actual use in game.

    Good luck and I hope this helps.
     
    DocJ likes this.
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I'm not sure how closely what you're doing is related to what I'm doing in my current game, but I'm generating tile maps using 3D tiles, and there may be a bit of useful info for you in my devlog here. If you have specific questions on any part of it, I'm happy to go into more detail.
     
    DocJ likes this.
  5. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    Bill, I see where you're going with that, and I appreciate the advice, but that is a bit more involved and in depth than what I'm trying to create. My project doesn't involve terrain in the sense that we would normally think of. The 9 tiles I am using can snap next to any tile in the set. Picture a 4x9 grid of 'spaces' represented by the 9 tiles in a specific design of my choosing. One tile will be the starting point, and one tile will represent the endpoint in that scene. Once on that end tile, the game will switch scenes to another scene, at which point when that scene is completed, the game will move on to the next level which will be another grid of pre-defined tiles in a specific layout of my choosing.

    The movement of the character will be determined by a random number (dice roll), but that's scripting for another day.

    Thank you again for your input. I appreciate all help and will try to be more specific as I ask questions on the forums.
    Doc
     
  6. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    Gotcha! Thanks. I'll get rid of the prefabs in the array and let you know how it works out. Thanks again for all the guidance.
     
    JoeStrout likes this.
  7. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    What I described is pretty basic and generic. Without knowing the specifics of your game it's hard to know how best to help you. Maybe it was so generic you can't see how that would work for you. There were other options I tossed in that you might not need. Also I used terrain and not tile, but the principal is the same. All you need is two small and easy functions. One to populate a map array, and one to read the map array and place the tiles. I think if you gave it a whirl you'd see that it is what you want or pretty close to it.

    If you don't want or need a procedurally built grid of tiles, the easiest way is to construct in the editor and disable and enable the tiles as you need. You could even construct the tiles for a level, make them children of an empty gameobject and enable and disable that object for each level.

    For a dice toss, you can start off with a simple random number. If you want an actual toss animation you can add in later, it's more productive to get the game working properly before getting a perfect dice roll animation.

    Let us know how your progressing.
     
    Last edited: Feb 23, 2017
    DocJ likes this.
  8. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    Thanks again. You're right, I had the same notion of building them as levels and then calling on them as I need them. I was just confused on how to assign them a grid position so I could have that random number move the character to a certain "space". So generating them through an array probably is altogether not what I'm looking for. I think. I have some homework on this to do this weekend. Again, thanks for the help. I'll keep in touch.