Limit AI head movement angle based on it's body

Hello, there.

I spent all day long try to do that, so… I’m giving up.

Here’s the deal, I want to my ia character be to turn it’s head without moving it’s body. I’d try using Quaternion.Euler:

    perception.eyeRot = Quaternion.LookRotation (eyeTgt - perception.eyeTransform.position);
        
    perception.curHeadRotX = Mathf.Lerp (perception.curHeadRotX, perception.eyeRot.eulerAngles.x, perception.angularHeadSpeed * Time.deltaTime);
    perception.curHeadRotX = Mathf.Clamp (perception.curHeadRotX, perception.headMaxRotationX.x, perception.headMaxRotationX.y);
        
    perception.curHeadRotY = Mathf.Lerp (perception.curHeadRotY, perception.eyeRot.eulerAngles.y, perception.angularHeadSpeed * Time.deltaTime);
    perception.curHeadRotY = Mathf.Clamp (perception.curHeadRotY, perception.headMaxRotationY.x, perception.headMaxRotationY.y);
        
    perception.eyeTransform.localRotation = Quaternion.Euler (perception.curHeadRotX, perception.curHeadRotY, 0f);

Brief explanation
perception = Class where I store the variables.
curHeadRotX = Atual head rotation in X axis, in degrees.
curHeadRotY = Atual head rotation in Y axis, in degrees.
headMaxRotationX = Vector2 that says the limit of the rotation in X axis.
headMaxRotationY = Vector2 that says the limit of the rotation in Y axis.

But, once it looks to the right, if not clamped, the vision rotation turns all around the ai character body.

Check out what happens:

So, I hope made myself clear enough, thanks in advance.

Have a look at the HeadLookController script.

1 Answer

1

If you character is rigged with a humanoid avatar, you can use the Unity built-in IK system to move your character head and make it look at a specific point in space, see:

And here is the documentation about the IK system: