- Home /
Questions about Grid Movement.
I already know how to teleport/ move to a specific grid but I have no idea how to detect if two grids are next to each other or on which grid the character is on. I tried using collisions but it didn't work with my script, in C#. Thank you.
Answer by Scribe · Apr 24, 2016 at 03:28 PM
It really depends on your setup, but I usually go with a 'data first, gameobjects later' approach. Make some class of gridspaces:
public class GridSpace{
Vector3 worldPosition;
//other variables
}
then make a world as a 2 dimensional array of type GridSpace
public class World{
GridSpace[,] grid;
}
and have some character which saves it grid space position:
public class Player{
int gridX;
int gridY;
}
now if our Player has variables gridX = 3, gridY = 5, and we want to go one space up, we just add one to gridY (depending on your orientation) and then lookup world.grid[gridX, gridY+1].worldPosition and we have the world position of the nextdoor grid space!
This is just one method, it can work perfectly well with collisions and raycasting if you set it up right, but I feel this is often less error prone.
Your answer
