65 lines
2.2 KiB
C#
65 lines
2.2 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
public class TunnelTraveler : MonoBehaviour
|
|
{
|
|
public float travelSpeed = 20f;
|
|
private bool _isTraveling;
|
|
public bool IsTraveling => _isTraveling;
|
|
|
|
public void StartTravel(TunnelNode start)
|
|
{
|
|
if (_isTraveling) return;
|
|
|
|
// [디버그 1] 상호작용한 노드의 연결 상태 확인
|
|
Debug.Log($"<color=white>[Travel] 시작 노드: {start.name} | 위: {(start.aboveNode != null)} | 아래: {(start.belowNode != null)}</color>");
|
|
|
|
List<Vector3> path = new List<Vector3>();
|
|
|
|
// 이동 방향 결정 (아래가 있으면 아래로, 없으면 위로)
|
|
bool goDown = start.belowNode != null;
|
|
TunnelNode curr = goDown ? start.belowNode : start.aboveNode;
|
|
|
|
while (curr != null)
|
|
{
|
|
path.Add(curr.transform.position);
|
|
curr = goDown ? curr.belowNode : curr.aboveNode;
|
|
}
|
|
|
|
// [디버그 2] 최종 경로 개수 확인
|
|
Debug.Log($"<color=yellow>[Travel] 생성된 경로 포인트 개수: {path.Count}</color>");
|
|
|
|
if (path.Count > 0)
|
|
{
|
|
StartCoroutine(Travel(path));
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("<color=red>[Travel] 이동할 경로가 없습니다! 노드 연결을 확인하세요.</color>");
|
|
}
|
|
}
|
|
|
|
private IEnumerator Travel(List<Vector3> path)
|
|
{
|
|
Debug.Log("<color=green>[Travel] 코루틴 이동 시작!</color>");
|
|
_isTraveling = true;
|
|
var cc = GetComponent<CharacterController>();
|
|
if (cc) cc.enabled = false;
|
|
|
|
foreach (var point in path)
|
|
{
|
|
Vector3 target = new Vector3(point.x, point.y - (cc ? cc.height / 2 : 0), point.z);
|
|
while (Vector3.Distance(transform.position, target) > 0.1f)
|
|
{
|
|
transform.position = Vector3.MoveTowards(transform.position, target, travelSpeed * Time.deltaTime);
|
|
yield return null;
|
|
}
|
|
transform.position = target;
|
|
}
|
|
|
|
if (cc) cc.enabled = true;
|
|
_isTraveling = false;
|
|
Debug.Log("<color=green>[Travel] 이동 완료!</color>");
|
|
}
|
|
} |