2026-06-22 캐릭터 영역별 스크립트
This commit is contained in:
44
Assets/02_Scripts/Npcs/FollowObject.cs
Normal file
44
Assets/02_Scripts/Npcs/FollowObject.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
// 대상(보통 플레이어)을 NavMesh 위에서 따라다니는 간단한 동행 스크립트.
|
||||
// 속도/회전속도/가속/높이(Base Offset) 등은 NavMeshAgent 컴포넌트에서 설정한다.
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
public class FollowObject : MonoBehaviour
|
||||
{
|
||||
[Header("Target")]
|
||||
[SerializeField] private Transform _target; // 비워두면 Camera.main 사용
|
||||
|
||||
[Header("Follow")]
|
||||
[SerializeField] private float _followDistance = 3.0f; // 이 거리 안이면 멈춤 (= Agent stoppingDistance)
|
||||
[SerializeField] private float _repathInterval = 0.2f; // 목적지 갱신 주기(초)
|
||||
|
||||
private NavMeshAgent _agent;
|
||||
private float _repathTimer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_agent = GetComponent<NavMeshAgent>();
|
||||
_agent.stoppingDistance = _followDistance;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
var target = ResolveTarget();
|
||||
if (target == null || !_agent.isOnNavMesh) return;
|
||||
|
||||
_repathTimer -= Time.deltaTime;
|
||||
if (_repathTimer > 0f) return;
|
||||
|
||||
_repathTimer = _repathInterval;
|
||||
_agent.SetDestination(target.position); // NavMesh가 지면/경사/장애물을 알아서 처리
|
||||
}
|
||||
|
||||
private Transform ResolveTarget()
|
||||
{
|
||||
if (_target != null) return _target;
|
||||
return Camera.main != null ? Camera.main.transform : null;
|
||||
}
|
||||
|
||||
public void SetTarget(Transform target) => _target = target;
|
||||
}
|
||||
2
Assets/02_Scripts/Npcs/FollowObject.cs.meta
Normal file
2
Assets/02_Scripts/Npcs/FollowObject.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c34c05a7f0add5248b2c4aff05789f4b
|
||||
Reference in New Issue
Block a user