55 lines
1.4 KiB
C#
55 lines
1.4 KiB
C#
using UnityEngine;
|
|
using DG.Tweening;
|
|
|
|
public class SmoothChildRotation : MonoBehaviour
|
|
{
|
|
[Header("References")]
|
|
public Transform targetChild; // Child object to rotate
|
|
public string playerTag = "Player"; // Tag for player detection
|
|
|
|
[Header("Rotation Settings")]
|
|
public float rotationDuration = 0.5f; // Time in seconds for rotation
|
|
public Ease rotationEase = Ease.OutQuad; // Easing type
|
|
public Vector3 targetRotationOnEnter = new Vector3(0, 0, -90);
|
|
public Vector3 targetRotationOnExit = new Vector3(0, 0, 0);
|
|
|
|
private Tween currentTween;
|
|
|
|
private void Start()
|
|
{
|
|
if (targetChild == null)
|
|
{
|
|
Debug.LogError("Target child is not assigned!");
|
|
enabled = false;
|
|
return;
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.CompareTag(playerTag))
|
|
{
|
|
RotateTo(targetRotationOnEnter);
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
if (other.CompareTag(playerTag))
|
|
{
|
|
RotateTo(targetRotationOnExit);
|
|
}
|
|
}
|
|
|
|
private void RotateTo(Vector3 targetRot)
|
|
{
|
|
// Kill any existing tween to avoid stacking
|
|
currentTween?.Kill();
|
|
|
|
currentTween = targetChild.DOLocalRotate(
|
|
targetRot,
|
|
rotationDuration
|
|
).SetEase(rotationEase);
|
|
}
|
|
}
|