🤔

Vector equality#

Despite the Vector == operator using an approximate equality, you can encounter cases where two vectors appear equal due to the lack of precision in the ToString implementation, which uses two decimal places.

Resolution#

When logging vectors, provide enough precision with a format string1.

-Debug.Log(vector);
+Debug.Log(vector.ToString("F7"));

You can also provide a format in interpolated strings:

-Debug.Log($"the position is: {position}");
+Debug.Log($"the position is: {position:F7}");

Alternatives#

If you need even less precision when comparing vectors, use a distance comparison:

if ((compareTo - position).sqrMagnitude < distance * distance)
{
    // "compareTo" and "position" are within "distance" of each other.
}
sqrMagnitude is preferred when calculating the actual distance is unnecessary.
  1. See standard numeric format strings for more information.