- Home /
Check if 2 objects positions are close enough
How would I go about checking to see if two objects positions are pretty close to each other? The reason being is that I have a grid based movement for my player piece and when it moves onto certain pieces, I want to check to see if their position is pretty much the same so I can then do certain functionality relating to that grid piece position. I say pretty much because as the piece moves now in set distances, it is never in a whole number (say its x position is 0, then when it moves 1 unit to the left, it becomes 0.9998 or 1.0002, etc.)
What I think would be the way to do it would be:
if(player.position.x >= grid.position.x - 0.1 || player.position.x <= grid.position.x + 0.1)
{
canDoStuff = true;
}
Then repeat for the y and z co-ordinates.
This seems like a long way around and was wondering if there was a unity function (min/max range?) that would make this easier?
So it would be in pseudocode: if player.position.x == grid.position.x give or take a few.
Cheers.
Check out "Vector3.Distance" in the manual. Is there a chance you could first check the many duplicates of this question to see what your issue is; you'll almost certainly find it!
Answer by 8r3nd4n · Jul 30, 2013 at 11:03 PM
Yes!
Thank you. Can't believe I didn't think of this as I have used it countless times before in different scenarios.