Unity Navgation System Miscellaneous Notes

stop pathfinding now

  • Using agent.isStopped=true, you can stop pathfinding, but there is a big delay, and the visual experience is very bad.

  • Use agent.enabled=false to stop pathfinding immediately by disabling the NavMeshAgent component. Because the component is disabled, it may cause other problems, such as losing the function of Obstacle Avoidance.

  • Use agent.ResetPath(), you can stop pathfinding, same as isStopped, there will be a big delay.

  • Using agent.destination = agent.transform.position; Although it looks awkward, it can meet the needs.

  • To use carve, you need to add NavMeshObstacle component to the object, enable this component when you need to stop, set Carve to true, and turn off NavMeshAgent at the same time (NavMeshObstacle and NavMeshAgent cannot coexist)

navAgent.SetDestination(point)

If the target point is not reachable, this will assign you a reachable point closest to the target point on the current Navmesh. This returns false if there are no reachable points.

SetDestination slow problem

If there are a large number of characters looking for paths at the same time, and with the reconstruction of the carve, the pathfinding will be time-consuming. The result is that the characters are stuck, which can be avoided by setting a large NavMesh.pathfindingIterationsPerFramevalue.
SetDestination itself is an asynchronous method, and unity limits the number of executions per frame by default. Through the above settings, the number of executions per frame is increased, but it is essentially a single-threaded method. When there are a large number of characters, the frame rate of the game will still be greatly reduced.

Judgment reachable

 NavMeshPath path = new NavMeshPath();
                    agent.CalculatePath(targetPos, path);
                    if (path.status == NavMeshPathStatus.PathComplete)
                    {
    
    
                        //reachable
                        agent.SetPath(path);
                        //agent.SetDestination(targetPos);//already CalculatePath,so use SetPath is more effective than SetDestination
                    }
                    else
                    {
    
    
                        //not reachable
                    }

Set up multiple pathfinding layers

  • For example, the requirement is that the pathfinding layer includes: ordinary roads and bridges.
  • The built-in default layers are Walkable, Not Walkable, and Jump. Add a new layer Bridge under Navigation->Areas. Note that the color of each layer in this interface is eventually reflected in the Gizmos display of the baked mesh. The process is as follows:
    • 1. Add NavMeshModifier to the object where the bridge is located, and set its Area Type property to Bridge. After the object where the bridge is located is selected, open Navigation->Object and set its Navigation Area to Bridge. Set Navigation Area to Walkable in other walking areas, and set Navigation Area to Not Walkable in obstacle areas
    • 2. NavMeshSurface bakes the walking area
    • 3. Set the walkable area for NavmeshAgent, such as:agent.areaMask = 1 << 0 | 1 << 3;
  • Finds the closest point in the navmesh from the specified point

NavMesh.SamplePosition(Vector3 sourcePosition, out AI.NavMeshHit hit, float maxDistance, int areaMask),bool True if the nearest point is found.reference: NavMesh.SamplePosition

NavMeshAgent pathfinding unit overlap( 大量带有NavMeshAgent的单位寻路到相同点时,在寻路过程中会重叠,到达目标点后也会重叠)

Method one, use Obstacle Avoidance.

The document says that Obstacle Avoidance uses rvo, but the effect is not very good. It is measured that the pushing and shaking caused by collisions between different objects are very serious.
If you want each unit to reach the end point better, the end point of each unit should not be set to the same point, you may need to pre-randomly set the corresponding position that each character will reach according to the pathfinding radius of each character , so that they don't push each other when pathfinding.

Method 2, use NavMeshObstacle to dig holes.

The specific process is to hang NavMeshAgent (Obstacle Avoidance is not enabled) and NavMeshObstacle components on the object at the same time (note that the two components cannot be enabled at the same time or uncontrollable teleportation will occur). Turn off the NavMeshObstacle component when moving, enable NavMeshAgent for pathfinding; turn off NavMeshAgent first after reaching the target point, and then enable the NavMeshObstacle component to dig holes, which will rebuild the Navmesh road network, and the pathfinding routes of other objects will also change and eventually go around Form an encirclement circle around the target.

According to the above content, it is known that NavMeshObstacle needs to be disabled and NavMeshAgent should be enabled to resume pathfinding after the object is dug. The relevant pseudocode is as follows:

navObstacle.enabled=false;
navObstacle.carving=false;
navAgent.enabled=true;

However, the fact is that if this is the case, it will also cause uncontrollable teleportation of the object. The reason is that the disabling of NavMeshObstacle is not completed immediately. The above code will cause NavMeshObstacle and NavMeshAgent to be turned on in the current frame. Therefore, after disabling NavMeshObstacle, you must delay at least 1 frame before enabling NavMeshAgent.

Reference: https://github.com/llamacademy/ai-series-part-33

other

A dots-based nav pathfinding on git: https://github.com/dotsnav/dotsnav

Guess you like

Origin blog.csdn.net/iningwei/article/details/130430312