Find NavMeshSurface Owner in Unity3D: A Step-by-Step Guide
Yes, you can find out a position's NavMeshSurface owner in Unity3D by using the following steps:
-
Get the NavMeshHit object by using NavMesh.SamplePosition method. This method will return the closest point on the NavMesh to the given position.
-
Get the NavMeshSurface component by using NavMesh.FindClosestEdge method. This method will return the closest edge of the NavMesh from the given position.
-
Get the NavMeshSurface component by using NavMesh.GetAreaFromName method. This method will return the NavMeshSurface component associated with the given NavMesh area name.
-
Check if the NavMeshSurface component returned in step 2 and step 3 is the same. If it is, then you have found the NavMeshSurface owner of the given position.
Here is the code for the above steps:
Vector3 position = new Vector3(1, 0, 1); // example position
NavMeshHit hit;
if (NavMesh.SamplePosition(position, out hit, 0.1f, NavMesh.AllAreas))
{
NavMesh.FindClosestEdge(hit.position, out hit, NavMesh.AllAreas);
NavMeshSurface navMeshSurface = NavMesh.GetAreaFromName(hit.mask.ToString())?.GetComponent<NavMeshSurface>();
if (navMeshSurface != null)
{
Debug.Log('NavMeshSurface owner found: ' + navMeshSurface.name);
}
else
{
Debug.Log('NavMeshSurface owner not found.');
}
}
else
{
Debug.Log('Position is not on NavMesh.');
}
Note: This code assumes that there is only one NavMeshSurface component associated with each NavMesh area name. If there are multiple NavMeshSurface components associated with the same NavMesh area name, then this code will only return one of them.
原文地址: https://www.cveoy.top/t/topic/ndsS 著作权归作者所有。请勿转载和采集!