Transform.rotation (Head) works for looking at player, but not when using Quaternion.Slerp?

I am trying to get an enemy’s head (animated bone) to look at the camera when within 90degree arc, and I got it working by googling a lot, but I simply can’t get it smooth. The Arc is working, and so does the “Looking” that sets if the enemy can see the Player (which is the camera). I’ve tried 100s of approaches and spent 6 hours on it as we speak, and I can’t seem to figure it out and getting stressed out about this, reaching out to you guys hoping for some help.

This script is called on the Enemy Object where I’ve coded the “AI”, not the Head nor Player itself.

Here is my Start() code:

void Start()
    {
        Head = transform.Find("_Head").gameObject;
        Player = GameObject.FindWithTag("Player");

        HeadStartRotation = Head.transform.rotation;
    }

Here is the working function (called within LateUpdate):

    void RotateHead(bool Looking)
    {
        Quaternion lookRotation = Quaternion.LookRotation(Head.transform.position - Player.transform.position, Vector3.up);

        if (Looking)
        {
            Head.transform.rotation = lookRotation * HeadStartRotation;
        }
        else
        {
            Head.transform.rotation = HeadStartRotation;
        }
    }

But when I do this, I don’t get any reaction at all, the enemy keep looking forward:

    void RotateHead(bool Looking)
    {
        Quaternion lookRotation = Quaternion.LookRotation(Head.transform.position - Player.transform.position, Vector3.up);

        if (Looking)
        {
            Head.transform.rotation = Quaternion.Slerp(Head.transform.rotation, lookRotation * HeadStartRotation, Time.deltaTime * 5f);
        }
        else
        {
            Head.transform.rotation = Quaternion.Slerp(Head.transform.rotation, HeadStartRotation, Time.deltaTime * 5f);
        }
    }

I am a beginner, and even simple transform.rotations are very hard for me to understand, but since I got it working with the rotation, I understood that using Slerp would do the same, but with a smooth rotation with Time.deltaTime, but obviously is not the case.

What am I doing wrong?

Works fine for me, must be something else wrong with your code.

8440484--1118597--Unity Rotation Problem1.jpg

That’s weird. Did you try it on an animated bone or a “normal” gameobject? It works perfectly fine with a random gameobject, but with a bone that is attached to an animated character it doesn’t work for me.

Could you please confirm this? Thanks!

(Please notice that in my code example the gameobject is called “_Head”, in the image above it’s “head”. I am aware of this, it is for example purpose and it’s not the issue)

Alright I got it working, finally!

I used a

Quaternion TempRotation;

And in Start() I assigned the original value:

 TempRotation = Head.transform.rotation;

And then it suddenly worked with the code below

    void RotateHead(bool Looking)
    {


        Quaternion lookRotation = Quaternion.LookRotation(HeadForArc.transform.position - Player.transform.position, Vector3.up);

        if (Looking)
        {
            Head.transform.rotation = Quaternion.Slerp(TempRotation, lookRotation * HeadForArcStartPosition, Time.deltaTime * 5f);
        }
        else
        {
            Head.transform.rotation = Quaternion.Slerp(TempRotation, HeadForArcStartPosition, Time.deltaTime * 5f);
        }

        // Store the rotation
        TempRotation = HeadForArc.transform.rotation;

      
    }

EDIT:
It worked fine until I’ve made the Character walk. Now the direction is wrong.

Usually animations override whatever transformations you apply to the object. Have you tried using Animation Rigging?

I’ve used the Built-in Ragdoll tool with a character w/animations I bought from the Unity Asset Store. I somehow managed to fix the issues with a lot of trial and error. I’ve made the head turn 90degrees while standing still in any direction, as well as while walking running (also tested with a patrol and wander script). I can’t say how I really fixed it as I am not experienced enough yet to know exactly, but I achieved what I wanted in the end.

For others who might be interested; I read almost in every similar forum question that one should use LateUpdate() to modify animations in the animator, as they happend between the Update and the LateUpdate process, but yet I achieved it with Update() which solved some other minor issues that happened with LateUpdate() in the process.

Thanks for contributing and showing interest in my question.