35 lines
1.0 KiB
C#
35 lines
1.0 KiB
C#
using UnityEngine;
|
|
|
|
public class SmoothLookAt : MonoBehaviour
|
|
{
|
|
[Tooltip("The target GameObject to look at.")]
|
|
public Transform target;
|
|
|
|
[Tooltip("How smoothly to rotate towards the target. Higher = slower.")]
|
|
public float rotationSmoothing = 5f;
|
|
|
|
[Tooltip("Offset in degrees to be applied after rotation (X, Y, Z).")]
|
|
public Vector3 rotationOffset = Vector3.zero;
|
|
|
|
void Update()
|
|
{
|
|
if (target == null)
|
|
return;
|
|
|
|
// Direction from this object to the target (in world space)
|
|
Vector3 direction = target.position - transform.position;
|
|
|
|
if (direction.sqrMagnitude < 0.0001f)
|
|
return;
|
|
|
|
// Base rotation to look at target
|
|
Quaternion targetRotation = Quaternion.LookRotation(direction);
|
|
|
|
// Apply offset in local rotation space
|
|
targetRotation *= Quaternion.Euler(rotationOffset);
|
|
|
|
// Smooth rotation
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmoothing);
|
|
}
|
|
}
|