diff --git a/Assets/02_Scripts/Npcs/FollowObject.cs b/Assets/02_Scripts/Npcs/FollowObject.cs index 34ae2486..f2f0a158 100644 --- a/Assets/02_Scripts/Npcs/FollowObject.cs +++ b/Assets/02_Scripts/Npcs/FollowObject.cs @@ -2,7 +2,7 @@ using UnityEngine.AI; // 대상(보통 플레이어)을 NavMesh 위에서 따라다니는 간단한 동행 스크립트. -// 속도/회전속도/가속/높이(Base Offset) 등은 NavMeshAgent 컴포넌트에서 설정한다. +// 속도/가속/높이(Base Offset) 등은 NavMeshAgent 컴포넌트에서 설정한다. [RequireComponent(typeof(NavMeshAgent))] public class FollowObject : MonoBehaviour { @@ -13,6 +13,10 @@ public class FollowObject : MonoBehaviour [SerializeField] private float _followDistance = 3.0f; // 이 거리 안이면 멈춤 (= Agent stoppingDistance) [SerializeField] private float _repathInterval = 0.2f; // 목적지 갱신 주기(초) + [Header("Rotation")] + [SerializeField] private bool _lookAtTarget = true; // 켜면 이동방향이 아니라 항상 Target을 바라봄 + [SerializeField] private float _lookSpeed = 8f; // 바라보기 회전 속도 (0이면 즉시) + private NavMeshAgent _agent; private float _repathTimer; @@ -27,11 +31,30 @@ private void Update() var target = ResolveTarget(); if (target == null || !_agent.isOnNavMesh) return; + // 목적지 갱신 (주기적) _repathTimer -= Time.deltaTime; - if (_repathTimer > 0f) return; + if (_repathTimer <= 0f) + { + _repathTimer = _repathInterval; + _agent.SetDestination(target.position); // NavMesh가 지면/경사/장애물을 알아서 처리 + } - _repathTimer = _repathInterval; - _agent.SetDestination(target.position); // NavMesh가 지면/경사/장애물을 알아서 처리 + // 회전: 켜면 항상 Target을 바라봄(에이전트 자동회전 끔), 끄면 이동방향을 바라봄(기본) + _agent.updateRotation = !_lookAtTarget; + if (_lookAtTarget) + FaceTarget(target); + } + + private void FaceTarget(Transform target) + { + Vector3 dir = target.position - transform.position; + dir.y = 0f; // 수평만 — 위아래로 안 기울게 + if (dir.sqrMagnitude < 0.0001f) return; + + Quaternion look = Quaternion.LookRotation(dir); + transform.rotation = _lookSpeed > 0f + ? Quaternion.Slerp(transform.rotation, look, _lookSpeed * Time.deltaTime) + : look; } private Transform ResolveTarget()