rotation causing crash problem

I have a problem with Unity crashing out or, dying to 2 fps occasionally while running the code below (during a LateUpdate)…
I can’t see anything wrong with it, and it works most of the time.
Both vectors are normalized (after setting .Y to zero, so the bone adjustment is in horizontal plane).

aimPivot is a bone in a character’s spine.
(this is part of some gun aiming code)

It seems like aither the acos() or the rotate is causing strange things to happen to the aimpivot transform… Unity sometimes spits out a bunch of errors, such as…

Quaternion To Matrix conversion failed because input Quaternion is invalid { 1.#QNAN0, 1.#QNAN0, 1.#QNAN0, 1.#QNAN0}

and

!sh->m_AABB.IsValid()
UnityEngine.Transform:RotateAround(Vector3, Single)
… for the code below.

also, it only runs the code if the magnitude of both vectors is >.9 (not zero length vectors)

what can cause this please ?

ang = Mathf.Acos(Vector3.Dot(pivotWeaponDirection, pivotTargetDirection));

sgn = Mathf.Sign(Vector3.Cross(pivotWeaponDirection, pivotTargetDirection).y);

ang *= sgn;

aimPivot.Rotate(Vector3.up, ang * Mathf.Rad2Deg, Space.World);

No sure, but in this line:

ang = Mathf.Acos(Vector3.Dot(pivotWeaponDirection, pivotTargetDirection));

Make sure the input vectors are unit-length, and also try clamping the argument to Acos() to the range [-1,1] to compensate for any numerical error.

thanks,
but those are already normalized, and clamped.

What I’m saying is that you need to clamp the argument to Acos() to the range [-1, 1]. Unless your code is different now that what you posted above, then no, it’s not currently clamped.

I’m not saying that’s what’s causing your problem here, but it’s a standard safeguard that should definitely be put in place here.

Also, you might try breaking the calculation into separate steps and adding some debug output. For example, output the result of the dot product operation, and also the result of the call to Acos(). (You might do this before adding any clamping or other additional safeguards, to have a better chance of finding out what the problem actually is.)