I know this sounds like a stupid question, as it seems pretty unlikely that something as fundamental as Vector3 could have such a huge bug in it that no one has noticed. I’m hoping someone can point out an obvious mistake I’m making.
I’m trying to compare two Vector3 for equality. In this specific example, I’m expecting both to be (0, 180, 0).
public Vector3 RightRotation = new Vector3(0, 180, 0);
private void Move()
{
Debug.Log("Rotation: (" + transform.rotation.eulerAngles.x + ", " + transform.rotation.eulerAngles.y + ", " + transform.rotation.eulerAngles.z + ")" + ", Rotation: (" + RightRotation.x + ", " + RightRotation.y + ", " + RightRotation.z + ")" + "; Equal: " + (transform.rotation.eulerAngles == RightRotation));
if (transform.rotation.eulerAngles == RightRotation)
{
Debug.Log("They are equal.");
}
}
When run this outputs the following to the console:
Rotation: (0, 180, 0), Rotation: (0, 180, 0); Equal: False
I am printing the individual floats here so there is no rounding taking place. The y values of both vectors are exactly 180. Yet, == returns false.
The documentation for Vector3.== says:
Returns true if the vectors are equal.
This will also return true for vectors that are really close to being equal.
It seems to me that something is not working right here.
This looks strange to me, too. O_O This is a shot in the dark, but maybe they differ by more than Mathf.Epsilon? I know that Mathf.Epsilon is defined to be the smallest possible number two floats can differ, and Mathf.Approximately uses this number to test for equality. Maybe Vector3's equality operator does the same thing on a per-component-basis, and it fails because one of the components is more different than epsilon, for some reason.
– CHPedersenI did end up using Vector3.Distance and comparing to a small value, and that works fine. I just don't understand how those Vector3s could not be equal.
– rmarra07