[SOLVED] How to check if float3 is zero.

Setting rotation on an object when using a Vector3 you can check if it has a zero value via:

var toTarget = PathToFollow.PathObjects[CurrentWayPointID].position - transform.position;
if (toTarget != Vector3.zero)  //to avoid the "Look rotation viewing vector is zero" exception
{
    //etc
}
float3 lookVector = autoPosition.destination - autoTranslation.Value;

//The following line gets the error:
//Look rotation viewing vector is zero
Quaternion rotationLookAt = Quaternion.LookRotation(lookVector);

However, if I attempt to wrap that like the Vector3:
if (lookVector != float3.zero)

I get an editor error: Cannot implicity convert type 'Unity.Mathematics.bool3 to ‘bool’

Wait, what? What? Nope, I’m lost. Help please.

math.all(lookVector != float3.zero)

3 Likes

Use math.all(bool3) or better and much faster - math.bitmask (for bool4)

1 Like

Thank you both very much. Seems like an odd thing to convert the float3 to a bool type… but then I am self taught and there is likely something fundamental I am missing.

Again, thank you very much.

Working example: (for the next person)

float3 lookVector = float3_Destination - float3_CurrentPosition;        
if (math.any(lookVector))
{
  //etc
}

math.any(float3.zero) is always false so u can just write if(math.any(lookVecotr))

2 Likes

oh, DUH.

Thank you!

Have a wonderful weekend

BTW, the first solution:
math.all(lookVector != float3.zero)
is incorrect as it returns false as soon as one component is zero.

2 Likes

Since float3 implements System.IEquatable, i would just write !lookVector.Equals(float3.zero) as it’s more readable than playing with bool3 math

2 Likes

You may use this: if(!lookVector.Equals(float3.zero))

Edit: @gebbiz was faster than me

2 Likes