- Home /
How to have a grid detect which square a player is in, in unity 2D
The gist of this is i want to have a very large grid with 1x1 squares and i want to be able to detect which grid in particular the player is closest to, say if the player is in the middle of two squares on the grid it will assume the player is in that one
I thought a way of doing it would be to use hundereds of box colliders but thought there may be an easier way of going about it, any help would be much appreciated
Answer by Runalotski · Nov 11, 2015 at 12:32 PM
you would convert the world coordinates to your grid coordinates.
so as you have a nice easy 1x1 grid if the player is at (1,0,4) in world coordinates then you know they are in the grid (1,4) if the grid lines up the world coordinates if not then you would add some offset to the code
if they are at (1.4,0,5.6) then you would round it to the nearest integer so the player is (1,6) on your grid
the easiest way to do this is to have the center of your zero square centered at 0,0,0 on the world coordinates.
Your answer
Follow this Question
Related Questions
Push only 1 box around at a time (2D) 1 Answer
Get position from Isometric TileMap 1 Answer
Rigidbody2d getting stuck when moving horizontally at random points! Can't figure it out :( 1 Answer
Block puzzle (1010) help 0 Answers
2D Grid Question 0 Answers