From f552be74be8a26c1ff50b0064ddc007e142df4b9 Mon Sep 17 00:00:00 2001 From: nakjun Date: Mon, 22 Jun 2026 13:33:33 +0900 Subject: [PATCH] =?UTF-8?q?2026-06-22=20=EC=9A=94=EC=A0=95=EC=9D=B4=20?= =?UTF-8?q?=ED=94=8C=EB=A0=88=EC=9D=B4=EC=96=B4=EC=97=90=20=ED=9A=8C?= =?UTF-8?q?=EC=A0=84=EC=9D=B4=20=EB=8F=99=EA=B8=B0=ED=99=94=EB=90=98?= =?UTF-8?q?=EB=8A=94=20=ED=98=84=EC=83=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/02_Scripts/Npcs/FollowObject.cs | 31 ++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) 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()