Practice_Unity/Assets/Scripts/Movements/Movement_Ground.cs
Seonkyu.kim 299af2279c 이동 작업
1. 플레이어 이동 작업
2. 무브먼트 통합 코드로 관리하게 작성
2025-09-19 17:46:08 +09:00

69 lines
2.4 KiB
C#

using UnityEngine;
using UnityEngine.AI; // Required for NavMeshAgent
public class Movement_Ground : Movement_Base
{
// Awake 메서드를 오버라이드하여 NavMeshAgent를 가져옵니다.
protected override void Awake()
{
base.Awake(); // BaseMovement의 Awake 호출
_agent = GetComponent<NavMeshAgent>();
if (_agent == null)
{
Debug.LogError("GroundMovement requires a NavMeshAgent component on the same GameObject!");
}
}
public override void SetSpeed(float speed)
{
if (_agent != null) _agent.speed = speed;
}
// BaseMovement의 SetDestination 추상 메서드를 구현합니다.
public override void SetDestination(Vector3 target)
{
if (_agent != null && _agent.isOnNavMesh)
{
_agent.isStopped = false; // 목적지 설정 전 에이전트가 멈춰있지 않도록 합니다.
_agent.SetDestination(target);
}
}
// BaseMovement의 Stop 추상 메서드를 구현합니다.
public override void Stop()
{
if (_agent != null && _agent.isOnNavMesh)
{
_agent.isStopped = true; // 에이전트를 명시적으로 멈춥니다.
}
}
// BaseMovement의 GetCurrentSpeed 추상 메서드를 구현합니다.
public override float GetCurrentSpeed()
{
return (_agent != null) ? _agent.velocity.magnitude : 0f;
}
// BaseMovement의 IsMoving 추상 메서드를 구현합니다.
public override bool IsMoving()
{
if (_agent == null || !_agent.isOnNavMesh) return false;
// 에이전트가 멈춰있지 않고, 경로가 있으며, 남은 거리가 stoppingDistance보다 크면 이동 중으로 간주합니다.
return !_agent.isStopped && _agent.hasPath && _agent.remainingDistance > _agent.stoppingDistance;
}
// Update 메서드에서 도착 체크 로직을 처리합니다.
void Update()
{
// 에이전트가 이동 중일 때만 도착 체크를 수행합니다.
if (IsMoving())
{
// 경로 계산이 완료되었고, 남은 거리가 stoppingDistance 이하면 도착으로 간주합니다.
if (!_agent.pathPending && _agent.hasPath && _agent.remainingDistance <= _agent.stoppingDistance)
{
Stop(); // 도착했으므로 에이전트를 멈춥니다.
}
}
}
}