Dynamic Collision Avoidance by NavMesh

Written by Zhu Wang on October 24, 2018

I talked about collision avoidance by using Unity NavMesh in my last post. The result is great if you have an agent who needs to avoid all the obstacles no matter when they move or stay stationary. The NavMesh agent module in Unity takes care of the pathfinding, and the obstacle module defines what objects should be labeled as the ones that agent needs to avoid during the pathfinding. However, when I implemented the NavMesh collision avoidance for our walking avatars, I found that Unity would not allow the agent module and the obstacle module to work simultaneously. This is terrible if we want the avatars to avoid each other and make new routes dynamically. If we activate the two module at the same time on one object, the pathfinding function seems not working but spinning around or walking in a circle with jitter. Unity official document says that they do not mix well and enabling both will make the agent trying to avoid itself.

Since the pathfinding is only needed when the agent estimates a collision is coming and needs to get a new path, we can enable the agent only when a possible collision is detected and have it disabled the rest of time. After the agent calculates a new route to avoid the collision, we can save the waypoints, disable the agent module, and have the obstacle module enabled back again for the next collision detection. The object will move along the waypoints until it detects the subsequent possible collision.

Here is what it looks like:

0 thoughts on “Dynamic Collision Avoidance by NavMesh”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.