Files
beyond/Assets/Scripts/PlayerAutoRotator.cs
2025-05-03 04:13:47 +02:00

37 lines
1007 B
C#

using UnityEngine;
namespace Beyond
{
public class PlayerAutoRotator : MonoBehaviour
{
public float rotationSpeed = 5f;
private Transform targetToLook;
private bool shouldRotate = false;
public void LookAtTarget(Transform target)
{
targetToLook = target;
shouldRotate = true;
}
private void Update()
{
if (!shouldRotate || targetToLook == null)
return;
Vector3 direction = (targetToLook.position - transform.position).normalized;
direction.y = 0;
if (direction == Vector3.zero) return;
Quaternion targetRotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
if (Quaternion.Angle(transform.rotation, targetRotation) < 1f)
{
shouldRotate = false;
}
}
}
}