Search Issue Tracker

By Design

Votes

0

Found in

2021.3.43f1

2022.3.46f1

6000.0.19f1

Issue ID

UUM-82365

Regression

No

NavMeshAgent.SamplePathPosition is returning a NavMeshHit with 0 mask when Nav Mesh Agent is nearing a corner of obstacle-culled NavMesh

--

-

Reproduction steps:
1. Open the attached “BugRepro” project
2. Open the “Assets/Scenes/SampleScene” Scene
3. Enter Play Mode
4. In the Game View, observe the “Capsule” GameObject approaching the corner of the obstacle culled NavMesh
5. In the Console, observe the logged NavMeshAgent.SamplePathPosition values

Expected result: The values should be non-zero
Actual result: There are values zero logged

Reproducible with: 1.0.0-exp.4 (2021.3.43f1), 1.1.5 (2022.3.46f1), 2.0.4 (6000.0.19f1)

Reproducible on: Windows 11, macOS 14.6.1 (M1 Max)
Not reproducible on: No other environments tested

  1. Resolution Note:

    TL;DR: When SamplePathPosition) returns false and the value 0 for hit.mask, you can call NavMesh.SamplePosition() at the point of the hit to obtain the area type where the path ends.

    In the reported case the agent is asked to move to a position outside of the NavMesh. The agent moves in a straight line towards the point on the edge of the NavMesh that is closest to the desired destination. The function agent.SamplePathPosition() is called with a maxDistance argument of 1f every frame throughout the agent’s movement.
    In the beginning, when the agent is farther than 1 unit from the edge of the NavMesh, the `SamplePathPosition()` call returns `false` to report that no edge is blocking the traversal of the path for that distance. In that case the area mask is taken from the place on the path that is 1 unit away from the agent.
    When the agent moves closer than 1 unit from the edge of the NavMesh, the SamplePathPosition() call returns a value of `true` to signify that `maxDistance` cannot be reached on the existing path. The `mask` property of the _out_ `hit` argument holds information about the area type that is blocking the sampling beyond the `hit.distance` range. The resulting `hit.mask` value of zero signifies that the sampling stops at the edge of the NavMesh, beyond which there is no NavMesh polygon. In that case you can call NavMesh.SamplePosition() at the point of the hit to obtain the area type where the path ends.

    Example code:

    ``` C#
    var terminatedEarly = agent.SamplePathPosition(agent.areaMask, 1f, out NavMeshHit hit);
    var lastAreaBeforeEnd = hit.mask;
    if (terminatedEarly)
    {
    NavMesh.SamplePosition(hit.position, out NavMeshHit endSample, 0.01f, agent.areaMask);
    lastAreaBeforeEnd = endSample.mask;
    }
    ```

    At the end of the agent’s movement the call to `agent.SamplePathPosition()` returns again a non-zero `mask` because at that point the agent has completed the movement on the path and it no longer has a path. The sampled area `mask` comes from the position of the agent.

    Please let us know if you have any questions.

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.