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?