Search Unity

Moving player based on 2D array map.

Discussion in 'Scripting' started by Tipico, Aug 29, 2015.

  1. Tipico

    Tipico

    Joined:
    Aug 4, 2015
    Posts:
    25
    Hi, I'd like to know how to move objects based on 2D Array map. I'm not familiar with 2D Array, but it seems really basic and fast performance to make a game, so I want to study about!

    I've been googling and watching Youtube tutorials, but I haven't find really helpful info. I've managed to randomly instantiate prefabs based on 2D array so far, but I don't know how to move the player based on the map.



    What I want to do is this.

    1. The player to move one grid (prefab) side way(A/B in the pic!) when I get left/right key up.

    2. Slide all prefab to down/up and get new prefab at the top/bottom when I get up/down key up. so that, it looks like the player moving forward/back forward like a treadmill
    Should I move the player inside of the array? Let's say player to be int =5 in array map?
    Or should I move the player using transform.translate then checked if the position of player is walkable int=0 in the array map?
    How can I know where the player position in the array, or where the A/B are?

    I'd really appreciate if you could give me any help :)

    Code (CSharp):
    1. public TileType[] tileTypes;
    2.         int[,] tiles;
    3.         int mapSizeX =10;
    4.         int mapSizeZ =30;
    5.    
    6.         void Start(){
    7.             GenerateMapDate();
    8.             GenerateMapVisual();
    9.         }
    10.    
    11.         void GenerateMapDate(){
    12.             tiles = new int[mapSizeX,mapSizeZ];
    13.            
    14.             for(int x=0; x<mapSizeX; x++){
    15.                 for(int z=0; z<mapSizeZ; z++){
    16.                     float treePerc = Random.value;
    17.    
    18.                     if(0 <= treePerc && treePerc <=0.8 && (x ==0 || x == mapSizeX-1))
    19.                         tiles[x,z]= 1;//grass
    20.                     else if(0.8 < treePerc && treePerc <=0.9 && (x ==0 || x == mapSizeX-1))
    21.                         tiles[x,z]= 2;//tree
    22.                     else if(0.9 < treePerc && treePerc <=1 && (x ==0 || x == mapSizeX-1)){
    23.                         if(x ==0 )
    24.                             tiles[x,z]= 3;//left apple
    25.                         else
    26.                             tiles[x,z]= 4;//right apple
    27.                     }
    28.                     else
    29.                         tiles[x,z] = 0;//road
    30.                 }
    31.             }
    32.         }
    33.    
    34.         void GenerateMapVisual(){
    35.             for(int x =0; x< mapSizeX; x++){
    36.                 for(int z=0; z< mapSizeZ; z++){
    37.                     TileType tt = tileTypes[tiles[x,z]];
    38.                     GameObject go = (GameObject)Instantiate(tt.tileVisualPrefab,
    39.                                                                  new Vector3(x,0f,z),Quaternion.identity);
    40.                 }
    41.             }
    42.         }
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Do you want to move the player or keep the player still & move all the tiles?
     
  3. MyIsaak

    MyIsaak

    Joined:
    May 23, 2013
    Posts:
    49
    Well a good way to control the players position on his 2D grid is to add or remove one from the index and then set the position of the player according to the grid. I assume you want a snapping effect, so you would use set the Transform position of the player directly, without interpolation. Here my concept.
    Code (CSharp):
    1. static class Map : MonoBehaviour {
    2. private int[,] map;
    3. public int width, height;
    4. public Map(int width, int height) {
    5.    this.width = width;
    6.    this.height = height;
    7.    map = new int[this.width,this.height];
    8. }
    9.  
    10. public SetPoint(int index, int x, int y) {
    11.    map[x,y] = index;
    12. }
    13.  
    14. public void Render() {
    15.    for(int x=0;x<width;x++){
    16.       for(int y=0;y<height;y++) {
    17.          switch(map[x,y]) {
    18.          case 0:
    19.             // Render air or grass
    20.             GameObject grass = GameObject.CreatePrimitive(PrimitiveType.Cube);
    21.             grass.transform.position = new Vector3(x,y,0);
    22.             grass.GetComponent<Renderer>().material.color = Color.green);
    23.          case 1:
    24.             // Render player
    25.             GameObject player = GameObject.CreatePrimitive(PrimitiveType.Cube);
    26.             player.transform.position = new Vector3(x,y,0);
    27.             player.GetComponent<Renderer>().material.color = Color.red);
    28.          case 2:
    29.             // Render object
    30.             GameObject wall = GameObject.CreatePrimitive(PrimitiveType.Cube);
    31.             wall.transform.position = new Vector3(x,y,0);
    32.             wall.GetComponent<Renderer>().material.color = Color.blue);
    33.          }
    34.       }
    35.    }
    36. }
    37. }
    38.  
    39. static class Player : MonoBehavior {
    40. private int x = 0;
    41. private int y = 0;
    42. public Player(int x, int y, Map m) {
    43.    this.x = x;
    44.    this.y = y;
    45.    m.SetPoint(1, this.x, this.y);
    46. }
    47.  
    48. public void Move(int x, int y) {
    49.    m.SetPoint(0, this.x, this.y);
    50.    this.x += x;
    51.    this.y += y;
    52.    m.SetPoint(1, this.x, this.y);  
    53. }
    54. }
     
    Tipico and Kiwasi like this.
  4. Tipico

    Tipico

    Joined:
    Aug 4, 2015
    Posts:
    25
    Sorry for the late reply, my Email had been buggy for while so I din't notice that I got reply :(
    I want to move the player only left or right, not forward nor backward.
    For the forward and backward movement, I want to player to be still & move all the tiles.
     
  5. Tipico

    Tipico

    Joined:
    Aug 4, 2015
    Posts:
    25
    Thank you, Thank you Thank you!!!!!
    I thought I was doomed !!
    I didn't notice this helpful reply until now due to that my Email notification has been crashed.

    I will post the result after I get home and try the code :D
     
  6. MyIsaak

    MyIsaak

    Joined:
    May 23, 2013
    Posts:
    49
    I noticed too late that you wanted all the tiles to move forward instead of the player. But I think I have enough framework for an efficient drawing system that supports multiple ground types. Hopefully you can elaborate this. I'd like to see how your project progresses. Contact me through Skype: myisaak123
     
  7. Tipico

    Tipico

    Joined:
    Aug 4, 2015
    Posts:
    25
    I'm sorry if my questions are too dumb:(

    (Q1)
    What are they?
    Are they methods ? Should I not write 'void' or 'variable names' ?
    They looks like some kind of variable type...but I could be wrong.
    Could you tell me the name of them ? reflection?

    (Q2)
    From 39 is another class, right?
    So I made a new class (script) and wrote down your code, but I can't call the 'SetPoint' from the Map class.
    Also, the Map class has an error that say "Class, struct, or interface method must have a return type".

    I'm not copy and paste your whole code because I have already create a map that I want, instead of copy & paste, I add some codes that I might need from your's.
    e.g. "Player class" , "SetPoint in the Map class"...

    And I change both class to be 'static' as you wrote.

    (Q3)
    Do I not need an Update function ?

    I really wish to, but my wifi is as slow as a snail, you might not be able to hear what I say :((
    Thank you for your generous offer !
     
  8. Tipico

    Tipico

    Joined:
    Aug 4, 2015
    Posts:
    25
    I think I found the answer of Q1 by myself just now.
    They are Constructors, aren't they?
    I'm embarrassed of my poor programming knowledge:oops:
     
  9. Tipico

    Tipico

    Joined:
    Aug 4, 2015
    Posts:
    25
    Code (CSharp):
    1. public SetPoint(int index, int x, int y) {
    2. map[x,y] = index;
    3. }
    A constructor has to have the same name of the class, right?
    So, this "SetPoint" ins't constructor...and it seems need a return type:rolleyes:

    Could you give me some hints? Please:oops::oops:
     
  10. matthewseaward

    matthewseaward

    Joined:
    Apr 12, 2013
    Posts:
    50
    Yup, you're right. SetPoint is meant to be a method, so it'll need to be 'Public void SetPoint' :)
     
  11. Tipico

    Tipico

    Joined:
    Aug 4, 2015
    Posts:
    25
    Thanks for the reply!
    OK, I did add 'void' to change it to be a method.

    Now, I have this error.
    " error CS0713: Static class `Map' cannot derive from type `UnityEngine.MonoBehaviour'. Static classes must derive from object"

    So I change "static" to "public" as normal class, is that ok?
    Is it must be a static class?

    To reference the map.setPoint from the Player class, I wrote "public Map map;" as a variable and drag & drop the Map script at the inspector.

    No error after this.
    But, nothing happen when I press play:confused:

    I think I should make "input method" somewhere...
    I'm not sure where is the right position as I'm not understanding this code well yet:(
     
  12. matthewseaward

    matthewseaward

    Joined:
    Apr 12, 2013
    Posts:
    50
    Yes, the Map class doesn't need to be static. It doesn't have any static methods and on the following lines
    Code (CSharp):
    1. public Player(int x, int y, Map m)
    2. {
    3.    this.x = x;
    4.    this.y = y;
    5.    m.SetPoint(1, this.x, this.y);
    6. }
    A reference to a Map object is being used. I'd remove the static keyword from both Player and Map.