How to check if Quaternion is NaN

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…

			if(Quaternion.IsNaN(sphereRtn))
			yield return null;

It produces this error.

Here is my original code and why i want to check for if Quaternion is NaN. (error on line 6)

		while(rtnToSnap){			
			float timeCrnt = Time.realtimeSinceStartup;	
			float deltaTime = timeCrnt - timeLast;
			float speed = 100;
			float step = speed * deltaTime;
			sphere.rotation = Quaternion.RotateTowards(sphere.rotation, sphereRtn, step);///ERROR IS HERE w/sphereRtn as NaN
			timeLast = timeCrnt;
			///use angle to dictate when coroutine should end
			float angle =  Quaternion.Angle(sphere.rotation, sphereRtn);
			if (angle < 0.01f)
			rtnToSnap = false; 
			yield return 0;
		}

This is the error I get, maybe once every 100 turns.

Thanks.

Where do you set sphereRtn? As this one is wrong, you need to correct that one.

I haven’t looked at the logic of your code, but to test for IsNaN I think you want something like this.

private bool IsNaN(Quaternion q) {
  return float.IsNaN(q.x) || float.IsNaN(q.y) || float.IsNaN(q.z) || float.IsNaN(q.w);
}
1 Like

I placed this line in, but still get the error.

			if(float.IsNaN(sphere.rotation.x)|| float.IsNaN(sphereRtn.x))
			yield return null;

Answer:

if (Single.IsNaN(myQuaternion.x) || Single.IsNaN(myQuaternion.y) || Single.IsNaN(myQuaternion.z) || Single.IsNaN(myQuaternion.w)) {
  // Quaternion has NaN values
}

Weird question but… How can a Quaternion be NaN?

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.

The only possibility is that sphereRtn it not valid. That’s why I asked about that one…

Yeah, because a properly initialized quaternion can never be NaN.

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.

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?