Search Unity

Grid based movement

Discussion in '2D' started by MasterElement, Jun 23, 2019.

  1. MasterElement

    MasterElement

    Joined:
    Jun 13, 2016
    Posts:
    134
    Hi, so I know this has been asked before, but I couldn't find any threads that gave an usable answer.
    So what is the best way to achieve this?
    I have found the concept of Tile map based a lot, bur not sure it's the way to go?
     
  2. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    i thing that you could have to do some math to achivie that when u get your input, the player only could move trow the grid, i mean, if the input axys t its positive, so the player, moves trow the grid, you would have to have one variable to save your current position, and make a grid trow colliders, boxes and circles, so, you can calculate the relative position of your player in front of the circles that would be the intersections of the grid, and you can make a paralel system where the input its being used to go trow the grid in a parallel way
     
  3. Cathal111

    Cathal111

    Joined:
    Jan 5, 2017
    Posts:
    14
    You could setup a Tilemap so that one square on that map = 1 Unity unit. Then just increase the players transform position by 1 in whichever direction you want to move. Units you don’t want to be able to walk on could have a Collider so you would not be able to walk onto them.
     
    Helladah likes this.
  4. Zitant

    Zitant

    Joined:
    May 26, 2019
    Posts:
    7
    I achieved this by creating a list of tiles in the Tilemap. Then, depending on how you move your character, you can check to see if if there is a tile where you are moving and set the position of your character to that tile's grid position. Unity has a nice function that will allow you to get the tiles center coordinates if you are looking to place your character right in the middle. How you do the animation or transition from tile to tile is up to you. I'm sure you could have a walking animation.
     
  5. Lynda7

    Lynda7

    Joined:
    Jun 27, 2019
    Posts:
    1
    Exactly..Creating list in the tilemap works.Then its totally on u according to your use and desires,how do u use it..So create a list in Tilemap,it would help