Get angular velocity?

Hi! IAstarAI has helpful information stored in velocity and desiredVelocity – it tells me whether or not my agent is currently moving. Is there something similar to tell if it’s currently rotating?

See, I want my tank unit to shoot only when it’s not moving OR rotating, but I haven’t found anything like that in the API.

Hi

This is included. You can replicate it by just storing the previous rotation of the agent and comparing it to the rotation of the agent during the current frame though. That’s essentially what the velocity field does.

I found that it wasn’t working for me, so I used the character controller rotation in FixedUpdate to calculate the angular velocity. The whole calculation actually ended up being pretty complicated (took it from a Unity forum somewhere):

    internal static Vector3 GetAngularVelocity(Quaternion foreLastFrameRotation, Quaternion lastFrameRotation)
    {
        var q = lastFrameRotation * Quaternion.Inverse(foreLastFrameRotation);
        if (Mathf.Abs(q.w) > 1023.999f / 1024.0f)
            return new Vector3(0, 0, 0);
        float gain;
        // handle negatives, we could just flip it but this is faster
        if (q.w < 0.0f)
        {
            var angle = Mathf.Acos(-q.w);
            gain = -2.0f * angle / ((float)Math.Sin(angle) * Time.deltaTime);
        }
        else
        {
            var angle = Mathf.Acos(q.w);
            gain = 2.0f * angle / ((float)Math.Sin(angle) * Time.deltaTime);
        }
        return new Vector3(q.x * gain, q.y * gain, q.z * gain);
    }

This does work for me. It didn’t work until I changed the first value to 1023.999f.