Dynamic Collision Avoidance by NavMesh
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: