Am I the only one? "Quaternion is invalid" and Uni

The following problem occurs both in Unity 2.5 and 2.6

I have a script-controllable vehicle with 8 wheel colliders; the vehicle is using asymmetric steering, so the rear 4 wheels rotate opposite the front 4 wheels. My script has a target point and target speed, and it uses PID controllers to generate thrust and steering angles that are fed to the wheel colliders. Thrust is only applied to the 2 rear wheels. The problem/bug is that after running for a few seconds, Unity freezes requiring me to kill the process. With nothing else running, I am pretty certain the problem is in the script.

After leaving it ‘frozen’ for almost 10 minutes, a bunch of messages pop in the console; most are: “Quaternion To Matrix conversion failed because input Quaternion is invalid”, but the system remain frozen so I can’t check values of variables or scroll in the console.

Things I have tried:

  • reduce time step - no change
  • change wheelcollider parameters - no change
  • move the initial position of my vehicle - error happens later, or at a different point
  • ensure steerAngle and torque are within bounds, using Mathf.Clamp - no change, in fact error occurs when steerAngle is near 0
  • setting steerAngle to 0 when it is near 0 - no change
  • setting steerAngle through the inspector while the game is running - works fine, but I want my script to be driving, so this doesn’t help me.

I have hit a wall, so to speak, at this point. To be sure, I did a search on the ‘invalid quaternion’ message, and suspiciously I have seen no response from Unity folks on the error, possible causes, or approaches to tracing such problems. In fact, most messages listing this error go un-answered.

I am not saying this is necessarily a bug, but is a problem that for me at least, has more-or-less halted any development with Unity, so any help or guidance would be greatly appreciated at this point!

I have attached the guiding script.

219271–7967–$drivestraddle_860.js (5.57 KB)

I didn’t see any problems with your script.
I think you are getting this error because you are forcing the physics engine to do something ‘unnatural’. But I can’t tell what without seeing your full set up. (even if I see it I probably won’t be able to, but other people might) What are the positions of the wheel colliders, how are they connected, etc. Perhaps you are forcing a movement against a joint…

ivkoni,

Thanks for following up. I am pretty certain that this is not the problem either- the layout is pretty physics-friendly and I have even lowered the lateral friction curves so the turning forces are pretty small.

The only progress I’ve made is that after moving things around, the problem occurs in a way that allows me to void killing Unity, so I can see debug print values; I’ll try to trace the problem that way.

Will report back if I figure it out.

How frustrating!

The Acos() function will return a NaN if the argument is > 1.0 or < -1.0. I was giving it the dot product of two unit vectors, which mathematically cannot be larger than 1.0 or less than -1.0

When printing its value, it was showing as 1.0 also.

However, because of numerical inaccuracies, the dot product ended up being greater than 1.0 but by a minute amount (like 1.0000001), that occurred when my vehicle was lined up with its target; that caused acos to return NaN, which then propagated to other parts of the code wrecking havoc everywhere.

The fix was simple, use Mathf.clamp() to ensure the dot product is within bounds and problem solved!!

Too bad acos doesn’t perform a rudimentary check on the input arguments!