Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Enter the 2020.2 Beta Sweepstakes for a chance to win an Oculus Quest 2.
    Dismiss Notice

[SOLVED] How to check if float3 is zero.

Discussion in 'Data Oriented Technology Stack' started by JamesWjRose, Nov 15, 2019.

  1. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    477
    Setting rotation on an object when using a Vector3 you can check if it has a zero value via:
    Code (CSharp):
    1.  
    2. var toTarget = PathToFollow.PathObjects[CurrentWayPointID].position - transform.position;
    3. if (toTarget != Vector3.zero)  //to avoid the "Look rotation viewing vector is zero" exception
    4. {
    5.     //etc
    6. }
    Code (CSharp):
    1.  
    2. float3 lookVector = autoPosition.destination - autoTranslation.Value;
    3.  
    4. //The following line gets the error:
    5. //Look rotation viewing vector is zero
    6. 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.
     
  2. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    100
    math.all(lookVector != float3.zero)
     
    Last edited: Nov 15, 2019
    siggigg and JamesWjRose like this.
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,085
    Use math.all(bool3) or better and much faster - math.bitmask (for bool4)
     
    JamesWjRose likes this.
  4. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    477
    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)
    Code (CSharp):
    1.  
    2. float3 lookVector = float3_Destination - float3_CurrentPosition;        
    3. if (math.any(lookVector))
    4. {
    5.   //etc
    6. }
    7.  
     
    Last edited: Nov 15, 2019
  5. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    100
    math.any(float3.zero) is always false so u can just write if(math.any(lookVecotr))
     
  6. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    477

    oh, DUH.

    Thank you!

    Have a wonderful weekend
     
  7. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    423
    BTW, the first solution:
    math.all(lookVector != float3.zero)
    is incorrect as it returns false as soon as one component is zero.
     
    JamesWjRose and Nyanpas like this.
  8. gebbiz

    gebbiz

    Joined:
    May 23, 2018
    Posts:
    14
    Since float3 implements System.IEquatable<float3>, i would just write !lookVector.Equals(float3.zero) as it's more readable than playing with bool3 math
     
    JamesWjRose likes this.
  9. wobes

    wobes

    Joined:
    Mar 9, 2013
    Posts:
    760
    You may use this:
    Code (CSharp):
    1. if(!lookVector.Equals(float3.zero))
    Edit: @gebbiz was faster than me
     
    Chmyke and JamesWjRose like this.
unityunity