Search Unity

How to check if Quaternion is NaN

Discussion in 'Scripting' started by renman3000, Dec 17, 2013.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    How do I check to see if a Quaternion is NaN? I have looked it up online but can not find an answer that pleases the engine.


    This does not work...
    Code (csharp):
    1.  
    2.             if(Quaternion.IsNaN(sphereRtn))
    3.             yield return null;
    4.  
    It produces this error.

    Here is my original code and why i want to check for if Quaternion is NaN. (error on line 6)
    Code (csharp):
    1.  
    2.         while(rtnToSnap){          
    3.             float timeCrnt = Time.realtimeSinceStartup;
    4.             float deltaTime = timeCrnt - timeLast;
    5.             float speed = 100;
    6.             float step = speed * deltaTime;
    7.             sphere.rotation = Quaternion.RotateTowards(sphere.rotation, sphereRtn, step);///ERROR IS HERE w/sphereRtn as NaN
    8.             timeLast = timeCrnt;
    9.             ///use angle to dictate when coroutine should end
    10.             float angle =  Quaternion.Angle(sphere.rotation, sphereRtn);
    11.             if (angle < 0.01f)
    12.             rtnToSnap = false;
    13.             yield return 0;
    14.         }
    15.  
    This is the error I get, maybe once every 100 turns.

    Thanks.
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Where do you set sphereRtn? As this one is wrong, you need to correct that one.
     
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I haven't looked at the logic of your code, but to test for IsNaN I think you want something like this.
    Code (csharp):
    1.  
    2. private bool IsNaN(Quaternion q) {
    3.   return float.IsNaN(q.x) || float.IsNaN(q.y) || float.IsNaN(q.z) || float.IsNaN(q.w);
    4. }
    5.  
     
    Marrt likes this.
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    I placed this line in, but still get the error.

    Code (csharp):
    1.  
    2.             if(float.IsNaN(sphere.rotation.x)|| float.IsNaN(sphereRtn.x))
    3.             yield return null;
    4.  
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    Answer:
    Code (csharp):
    1.  
    2. if (Single.IsNaN(myQuaternion.x) || Single.IsNaN(myQuaternion.y) || Single.IsNaN(myQuaternion.z) || Single.IsNaN(myQuaternion.w)) {
    3.   // Quaternion has NaN values
    4. }
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Weird question but... How can a Quaternion be NaN?
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    I am not sure.
    In my script, I am rotating a transform to match a specific rotation or Quaternion value. I guess, for some reason which I have not picked up, the value of this target rotation, is occasionally missed.
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    The only possibility is that sphereRtn it not valid. That's why I asked about that one...
     
  9. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Yeah, because a properly initialized quaternion can never be NaN.
     
  10. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    What is really wierd is, I made some code adjustments last night and it started happening much more frequently. I added in a print(sphereRtn); just after the place where it should be assigned, now, it rarely happens.

    Anyhow, not too concerned.
    Logic rules.
    Spock.
     
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I find it irritating how much you like it to talk about the issue and what you are experiencing. It is obvious that sphereRtn is the problem in your code. You know the cause, why do you try to resolve it somewhere else?