- Home /
How does NavMeshAgent.SamplePathPosition work?
I'm trying out the Unity (Pro) NavMeshAgent and I've hit a road block with the NavMeshAgent.SamplePathPosition() function.
From what I understand, the purpose of this function is to examine a specific area of the path that is X units in front of the agent along the path to the agent's destination. Is this correct? (If not please point me in the right direction)
If my presumption is correct, I've set up an object (myAgent) and applied the NavMeshAgent component, and I have created a target (myTarget) for it in my scene.
Here is my code that is attempting to test the location 10 units in front of my agent along the Agent path:
NavMeshHit navhit;
myAgent.SamplePathPosition(NavMesh.GetNavMeshLayerFromName("Default"), 10f, out navhit);
Debug.Log(navhit.position); //this is the same as myAgent.transform.position
However, it seems that no matter what value for the MaxDistance (the 10f), the position that is being tested is always the location of the Agent. Am I totally using this function incorrectly?
My ultimate goal is to look ahead on the agent path and get the Vector3 position of the navhit.
Thank you for any help you can provide.
Answer by spolglase · Sep 04, 2013 at 08:15 PM
The method actually works perfectly fine. The documentation is just terrible. You need to bitshift the layer. Typically you are trying to query the default layer so it would look something like this:
1 << NavMesh.GetNavMeshLayerFromName("Default")
Thanks! It only took almost exactly 1 year for the answer :)
Your answer
Follow this Question
Related Questions
NavMesh area, walkable but not accessible. 2 Answers
NavMeshAgent and NavMeshObstacle together, unit jumping a small distance on toggle 2 Answers
Is there a limit on how far a navmesh agent can go? 0 Answers
Getting a navmeshagent back on a navmesh. 1 Answer
SetDestination's and CalculatePath's Path State Return Works Too Late 1 Answer