Comparing local direction with global direction gives inconsistencies


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


 private bool IsPlayerZWorldY()
    			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.


Finally found it. This solved it for me: Vector3 == comparison efficiency and float precision - Questions & Answers - Unity Discussions