Comparing local direction with global direction gives inconsistencies
Hi,
I have cube that you can move around by flipping it over it's sides. When turning is done and the cube is on a side standing still, I calculate it's direction and set some values accordingly by using scripts like this for each axis:
if (IsPlayerZWorldY())
{
//setting values etc here
}
and:
private bool IsPlayerZWorldY()
{
print(transform.forward);
return transform.forward == Vector3.up || transform.forward == Vector3.down;
}
Now the weird thing is, 99,9% of the times, this works totally fine. But sometimes, even though the print statement above the return statement prints (0, 1, 0), it won't return true. Transform.forward being (0, 1, 0) is the same as it being Vector3.up. So what gives? Why does it sometimes return true and sometimes not? If anybody has any ideas on this you'd be helping me out big time. Been banging my head against this one for a long time.
Thanks!
Answer by Jointy87 · Nov 16, 2020 at 07:13 PM
Finally found it. This solved it for me: https://answers.unity.com/questions/395513/vector3-comparison-efficiency-and-float-precision.html