Check if GameObject is x units next to GameObject2

Please, help needed…
Like in title: I want to check is there GameObject X units away from another GameObject
For example
If there is GameObject 10 units away from GameObject2 in X axis, do something.

Thanks for any help in this topic.

    float distance = Vector3.Distance (object1.transform.position, object2.transform.position);

Perhaps you will have to define the size of a unit as a float and then you can figure the total distance? Not entirely sure how you’d go about this if only moving at right angles?

What Josh says is right on but keep in mind you cannot compare floating point values for equality due to floating point imprecision.

Instead it is always best to compare for crossing that distance by keeping the previous distance from the previous frame and comparing it to this one.

1 Like