터널 연장 로직 개선
This commit is contained in:
@@ -4,87 +4,62 @@ using System.Collections.Generic;
|
||||
|
||||
public class TunnelTraveler : MonoBehaviour
|
||||
{
|
||||
public float travelSpeed = 25f; private bool _isTraveling = false;
|
||||
// 외부에서 읽기 전용으로 접근할 수 있게 합니다.
|
||||
public float travelSpeed = 20f;
|
||||
private bool _isTraveling;
|
||||
public bool IsTraveling => _isTraveling;
|
||||
|
||||
private CharacterController _controller;
|
||||
private Rigidbody _rigidbody;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_controller = GetComponent<CharacterController>();
|
||||
_rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
private IEnumerator TravelRoutine(List<Vector3> path, TunnelNode startNode)
|
||||
{
|
||||
_isTraveling = true;
|
||||
|
||||
if (_controller != null) _controller.enabled = false;
|
||||
if (_rigidbody != null) _rigidbody.isKinematic = true;
|
||||
|
||||
// 1. 캐릭터 높이 보정값 계산
|
||||
float heightOffset = 0f;
|
||||
if (_controller != null) heightOffset = _controller.height / 2f;
|
||||
|
||||
// 2. [입구 정렬] 시작 노드의 정확한 중앙 위치로 플레이어를 즉시 이동시킵니다.
|
||||
// 플레이어의 '발바닥' 위치를 노드 중앙보다 heightOffset만큼 아래로 맞춤
|
||||
Vector3 entryPosition = new Vector3(startNode.transform.position.x,
|
||||
startNode.transform.position.y - heightOffset,
|
||||
startNode.transform.position.z);
|
||||
|
||||
// 시작 지점으로 순간이동 또는 아주 빠르게 정렬
|
||||
transform.position = entryPosition;
|
||||
|
||||
// 3. 경로 이동 시작
|
||||
foreach (Vector3 targetPos in path)
|
||||
{
|
||||
Vector3 adjustedTarget = new Vector3(targetPos.x, targetPos.y - heightOffset, targetPos.z);
|
||||
|
||||
while (Vector3.Distance(transform.position, adjustedTarget) > 0.01f)
|
||||
{
|
||||
transform.position = Vector3.MoveTowards(transform.position, adjustedTarget, travelSpeed * Time.deltaTime);
|
||||
yield return null;
|
||||
}
|
||||
transform.position = adjustedTarget;
|
||||
}
|
||||
|
||||
if (_rigidbody != null) _rigidbody.isKinematic = false;
|
||||
if (_controller != null) _controller.enabled = true;
|
||||
|
||||
_isTraveling = false;
|
||||
}
|
||||
|
||||
// StartTravel 함수에서 startNode를 코루틴에 넘겨주도록 수정
|
||||
public void StartTravel(TunnelNode startNode)
|
||||
public void StartTravel(TunnelNode start)
|
||||
{
|
||||
if (_isTraveling) return;
|
||||
|
||||
List<Vector3> path = GeneratePath(startNode);
|
||||
// [디버그 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)
|
||||
{
|
||||
// startNode 정보를 함께 넘김
|
||||
StartCoroutine(TravelRoutine(path, startNode));
|
||||
StartCoroutine(Travel(path));
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("<color=red>[Travel] 이동할 경로가 없습니다! 노드 연결을 확인하세요.</color>");
|
||||
}
|
||||
}
|
||||
|
||||
// GeneratePath 함수는 기존과 동일하게 유지합니다.
|
||||
private List<Vector3> GeneratePath(TunnelNode startNode)
|
||||
private IEnumerator Travel(List<Vector3> path)
|
||||
{
|
||||
List<Vector3> path = new List<Vector3>();
|
||||
TunnelNode currentNode = startNode;
|
||||
HashSet<TunnelNode> visited = new HashSet<TunnelNode>();
|
||||
Debug.Log("<color=green>[Travel] 코루틴 이동 시작!</color>");
|
||||
_isTraveling = true;
|
||||
var cc = GetComponent<CharacterController>();
|
||||
if (cc) cc.enabled = false;
|
||||
|
||||
while (currentNode != null && !visited.Contains(currentNode))
|
||||
foreach (var point in path)
|
||||
{
|
||||
visited.Add(currentNode);
|
||||
TunnelNode exitNode = currentNode.otherEndOfThisSegment;
|
||||
if (exitNode == null) break;
|
||||
|
||||
path.Add(exitNode.transform.position);
|
||||
currentNode = exitNode.connectedNode;
|
||||
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;
|
||||
}
|
||||
return path;
|
||||
|
||||
if (cc) cc.enabled = true;
|
||||
_isTraveling = false;
|
||||
Debug.Log("<color=green>[Travel] 이동 완료!</color>");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user