Lock the head rotation ?

Hi,
I’m working on a model where the head of the doll rotate to look at target. What i what is to make the rotation of the head between two values.

    public Transform Target;
    public Vector3 Offset;
    public Transform head;

    void Start()    {
        anim = GetComponent<Animator>();
        head = anim.GetBoneTransform(HumanBodyBones.head);

    }

    void LateUpdate()    {
        head.LookAt(Target.position);
        head.rotation = head.rotation * Quaternion.Euler(Offset);

    }

I think what you’d want to do is store two quaternions, one with the original rotation and one with the target rotation.

Once you have two quaternions you can use quaternion.lerp to create any rotation between those two.

For instance, here’s some code that should set the rotation to a value between the 2 rotation points:

    public Transform Target;

    [Range(0.0,1.0)]
    public float TimeValue = 0.5f;

    private Quaternion OriginalRotation;
    private Quaternion TargetRotation;
    private Transform head;

    void Start() 
    {
            anim = GetComponent<Animator>();
            head = anim.GetBoneTransform(HumanBodyBones.head);
       
            OriginalRotation = head.rotation;
            head.LookAt(Target.position);
            TargetRotation = head.rotation;
    }
   
    void LateUpdate() 
    {
            // Set the current rotation
            head.rotation = Quaternion.Lerp( OriginalRotation , TargetRotation , TimeValue );
    }

Modify ‘TimeValue’ in the inspector to move between rotations. I haven’t tested this code but it should work.

1 Like

Thank you tonemcbride.