유니티
[유니티]navMesh 오브젝트 사이 거리 / navigation 오브젝트 사이 거리 / nav 거리값
Krapboss
2023. 11. 6. 04:33
출처
https://docs.unity3d.com/ScriptReference/AI.NavMesh.CalculatePath.html
Unity - Scripting API: AI.NavMesh.CalculatePath
Use this function to avoid gameplay delays by planning a path before it is needed. You can also use this function to check if a target position is reachable before moving the agent. This function is synchronous. It performs path finding immediately which c
docs.unity3d.com
using UnityEngine.AI;
//너무 자주 호출하면 좋지 않습니다.
//현재 네비메쉬를 기반으로 거리값을 계산해서 넘겨줍니다.
public float GetPathDistance(Vector3 from, Vector3 to)
{
//경로 저장을 위한 변수
NavMeshPath path = new NavMeshPath();
if(!NavMesh.CalculatePath(from, to, NavMesh.AllAreas, path))
{
Debug.LogWarning("네비 메쉬가 존재하지 않습니다.");
return Mathf.Infinity;
}
float distance = 0;
for (int i = 0; i < path.corners.Length - 1; i++)
distance += Vector3.Distance(path.corners[i] , path.corners[i+1]);
Debug.LogWarning($"현재 경로 거리값 제공 : {distance}");
return distance;
}
- 내부 함수내용만 사용하시면 됩니다.