Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Alternative to Unity's NavMesh system where the agents avoid each other.

License

Notifications You must be signed in to change notification settings

bmjoy/CustomNavMesh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

58 Commits

Repository files navigation

Custom Nav Mesh

Alternative to Unity's NavMesh system where the agents avoid the other non moving agents in their pathing. This still uses the official navigation system but you have to use it's components instead. Compatible with NavMeshComponents.

How it works

How do the agents avoid others? The Unity's NavMeshAgent only goes around NavMeshObstacles and ignores the other agents in their path. By disabling NavMeshAgent and enabling NavMeshObstacle when an agent is not moving noticeably, the agents now avoid other non moving agents in their pathing.

1. Set destination to blue target. Unity's NavMeshAgent on the left, and CustomNavMeshAgent on the right.

What if a moving agent collides against a stopped agent that's currently in obstacle mode? Normally, it wouldn't push the agent in the obstacle mode. However, if you:

  • duplicate the baked surface, obstacle and agents
  • make the duplicated agents switch between NavMeshAgent and NavMeshObstacle instead of the original ones
  • have the original agent copy the duplicated one's velocity, and the duplicated one copy the original's position at every frame

You can have them push the others. That's what this CustomNavMesh system does "under the hood", you just have to use it's custom components, which are identical to the original ones, making the transition seamless.

2. Overlapping agents by throwing one against the others. The agents are red and the obstacles blue. Unity's NavMeshAgent against agents that switched to NavMeshObstacle on the left, and CustomNavMeshAgents on the right. This is just a showcase, if you're trying to do something similar you should use colliders and physics instead since this system is simply used to resolve agent overlap.

Instead of doing this, why not just switch the obstacle to agent? That wouldn't work because that switch isn't instant, it takes at least two frames.

What are the disadvantages? Well, every nav mesh component is duplicated, which makes it less performant. Shouldn't make a different unless you have a lot of agents.

3. The duplicated NavMesh components. The hidden components are on the right side.

Custom classes

CustomNavMesh – you can choose the hidden game objects relative position and whether or not they are rendered by accessing the class or through it's singleton present in the scene.

4. Changing CustomNavMesh fields.

  • CustomNavMeshSurface – add to each surface that is going to be baked. The surfaces need to be rendered meshes and not Physics Colliders or Terrains.
  • CustomNavMeshObstacle – replacement for NavMeshObstacle.
  • CustomNavMeshAgent – replacement for NavMeshObstacle. It has some added properties:
    • Time to Block – time in seconds needed for the agent to start blocking (to make others go around it), assuming it hasn't surpassed the Unblock Speed Treshold during the interval.
    • Unblock Speed Threshold – speed at which the agent stops blocking and moves instead.
    • Block Refresh Interval – time in seconds needed for the agent to check if it should stop blocking.
    • Min Distance Boost to Stop Block – When block is refreshed, this is the minimum distance the newly calculated reacheable position must be closer for the agent to stop blocking.
    • The rest have to do with the blocking obstacle carving and are identical to the NavMeshObstacle carving settings.

How To Get Started

Clone or download this repository and open the project in Unity. Alternatively, you can copy the contents of Assets/CustomNavMesh to an existing project.

The scenes used for some of the GIFs are available in the Assets/Examples folder. Just hit the Space Bar in play mode to test it.

Note: This project was created using Unity 2019.4 LTS version. Tested in PC Standalone, Android and WebGL.

To be implemented

  • Every time a agent in block mode does a refresh (tries to see if it should unblock), SamplePosition and CalculatePath are called. Both of these functions are synchronous and run on the main thread. I could divide them by the frames or make them run in separate threads to avoid ms spikes.
  • Import more methods and properties from the NavMesh and NavMeshAgent to the CustomNavMesh and the CustomNavMeshAgent, respectively.
  • Create custom versions for the NavMeshComponents' components.

Until now, I have only added the things I need for another project, but that should be enough for most cases. If you need help implementing some of these or have any question, just message me and I'll try to help. I'm also interested in any tips or suggestions you may have, cheers!

About

Alternative to Unity's NavMesh system where the agents avoid each other.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

AltStyle によって変換されたページ (->オリジナル) /