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.


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


You can also provide a format in interpolated strings:

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


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.