Two Vector3 comparations doesnt work.

Hi guys,

this must be a really stupid problem.
When I print Vector3 values as bellow, console says it is (0.0, 0.0, 0.0):

print (transform.position - player.transform.position - cameraPosition);

when I try to compare two Vectors according to this 1 this code doesnt work:

if(transform.position - player.transform.position - cameraPosition == Vector3.zero)
	print("yes");

Anybody knows why?

When you print a Vector3, the numbers shown are formated to display only 1 decimal point.

Each component is close to zero, but not zero. Try printing each component like:

Vector3 aux = transform.position - player.transform.position - cameraPosition;

print(aux.x);
print(aux.y);
print(aux.z);

And you’ll see it.

Anyway, comparing floats for equality is a known problem, you should never do that. Instead, check if the 2 values are close enough or, in other words, if the distance between 2 floats is close enough to zero.

There are multiple approachs for this, in your case I’d do:

Vector3 aux = transform.position - player.transform.position - cameraPosition;

if( aux.sqrMagnitude < 0.01f) //set a proper value, 0.01f is just an example
     print("yes");