How to rotate a NPC to face player

Anyone have a solution to rotate an NPC to face the player? The NPC has an Animator with a blend tree, and so far, none of the Unity-provided solutions are working (I’m using the NPC code from the Unities old Stealth game). When I set the angularSpeed param on the Animator the correct animation clip plays (turnOnSpot) but it slides the NPC. Also, even the the player is stationary, the NPC can never seem to determine when it is facing the player and its rotation is within tolerance (say +/- 5 degrees) to stop rotating. It seems like the animation is batting with code that uses Quaternion.Slerp to update the NPC rotation. Maybe there’s some combination of using Update and LateUpdate to do this? Not sure.

try this…

Some tweaking will help

using UnityEngine;

public class RotateToFacePlayer : MonoBehaviour
{
    public Transform player;
    public Animator animator;
    public float rotationSpeed = 3.0f;
    public float rotationTolerance = 5.0f;

    private void Update()
    {
        if (player == null || animator == null)
        {
            return;
        }

        // Calculate the direction to the player
        Vector3 playerDirection = player.position - transform.position;
        Quaternion targetRotation = Quaternion.LookRotation(playerDirection);

        // Calculate the angle between the current rotation and the target rotation
        float angle = Quaternion.Angle(transform.rotation, targetRotation);

        // Update the Animator parameter to control the turning animation
        animator.SetFloat("TurnSpeed", angle);

        // Rotate the NPC smoothly
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);

        // Stop rotating if the angle is within the tolerance
        if (angle <= rotationTolerance)
        {
            animator.SetFloat("TurnSpeed", 0f);
        }
    }
}