Detecting whether an object is "round-abouts" at a position/rotation.

Hi,

I am having issues with comparing positions of objects, with use with Vector3.SmoothDamp:

while (CoverUser.transform.position != desiredPosition)
            {
                var movementSpeed = new Vector3();
                CoverUser.transform.position = Vector3.SmoothDamp(CoverUser.transform.position, desiredPosition, ref movementSpeed, 0.1f);
                yield return new WaitForFixedUpdate();
            }

The above code means the coroutine is ALWAYS running, because the position of the object is out by ~0.001, and therefore not equal to the desired position.

Is there a way to compare if the position of the object is round-abouts the same as another set position? I don’t want to have to convert everything to Int32’s using Convert.ToInt32() if possible.

Thanks in advance!
Rich

Use the static Vector3.distance function to get the distance between your two positions. If that distance is within some tolerance, consider them to be the same.

(CoverUser.transform.position -desiredPosition).sqrDistance < desiredDistance*desiredDistance)

This should be the best performance-wise. the sqrDistance instead of just distance is that square-roots are computational heavy and multiplication isn’t :smile:.(not a must though, depends how often and especially on how many objects you want this done).

Also remember that if you do you CoverUser.transform.position on many places in you script, you should think about storing it in a temporary Vector3 also. For performance :smile: