Title pretty much sums it up. I am using the main Camera position and sampling it’s nearest position to the NavMesh (since it may move freely and potentially off of the main path). I then move an agent to this sampled position, and perform a CalculatePath on the agent.
The spot that SamplePosition is selecting is nowhere near the main Camera. There are many significantly closer points it could choose.
Here is a screenshot of what it looks like in Editor (Blue is Main Camera, red is SamplePosition result):
And here is the piloting code:
private void PlaceAgentOnPath()
{
NavMeshHit hit;
//Sample the position to determine the nearest NavMesh point the camera is close to
NavMeshQueryFilter filter = new NavMeshQueryFilter();
//Set the area mask to only include the Player layer (layer 4)
filter.areaMask = 1 << 4;
if (NavMesh.SamplePosition(Camera.main.transform.position, out hit, 25f, filter))
{
agent.transform.position = hit.position;
}
else
{
Debug.Log("No valid NavMesh point found within range!");
}
}
Note that the Pink path in the screenshot is not being used, the Green (Player) path is being used on Layer 4.
Let me know if you have any suggestions as to how to remedy this, or an alternative to guarantee I get the nearest point from the camera each time. Thank you.
Are you sure that using the “Camera.main.transform.position” does make sense here?
It would be best if you transformed your camera position to terrain/room position by e.g. casting a ray to the ground, otherwise, this wouldn’t work correctly.
What exactly would that change? I couldn’t specifically raycast downwards towards the NavMesh considering the whole use-case of this solution is to still create a navigation path when the player object is OFF of the NavMesh instead of within bounds.
I wouldn’t think this would be a local/global position based issue either considering I am using the global values for the agent, camera, and detected hit on the NavMesh from SamplePosition.
Can you elaborate more on what the difference is between my sampled position being on ground-level rather than ~2 units above ground like the camera?
I suggested this because camera positions are normally not a good place to start a SamplePosition call in a 3d game. Also, I can’t identify if this is a 2d or 3d game or a 2d/3d navmesh, maybe you have some y,z positions exchanged. I use SamplePosition all the time and it’s working just fine.
This is a 3D scene and the NavMesh is technically only 2-dimensional since there is no elevation.
I make sure to only modify global positions to avoid this being an issue of local/global position, is SamplePosition supposed to return a global space point, or is it one local to the NavMeshSurface?
I am also curious as to why using a camera position isn’t a valid technique, you’ve suggested against it but I would still like to know why this is the case. Is it due to the extra dimension that the NavMesh doesn’t have being 2D, or something else entirely?
Thanks for clarifying. There seems to be a misunderstanding with my goal, which is specifically to get the nearest point to the player on the NavMesh. I use the Camera position since this is attached to the player and is easy to access. For this reason, the Camera is the point I use.
I will do further testing to try and determine why it is giving me the wrong point. Thanks.