Rasagar/Library/PackageCache/com.unity.ai.navigation/Documentation~/MixingComponents.md
2024-08-26 23:07:20 +03:00

4.9 KiB
Raw Blame History

Using NavMesh Agent with Other Components

You can use NavMesh Agent, NavMesh Obstacle, and Off Mesh Link components with other Unity components too. Heres a list of dos and donts when mixing different components together.

NavMesh Agent and Physics

  • You dont need to add physics colliders to NavMesh Agents for them to avoid each other
    • That is, the navigation system simulates agents and their reaction to obstacles and the static world. Here the static world is the baked NavMesh.
  • If you want a NavMesh Agent to push around physics objects or use physics triggers:
    • Add a Collider component (if not present)
    • Add Rigidbody component
      • Turn on kinematic (Is Kinematic) - this is important!
      • Kinematic means that the rigid body is controlled by something else than the physics simulation
  • If both NavMesh Agent and Rigidbody (non-kinematic) are active at the same time, you have race condition
    • Both components may try to move the agent at the same which leads to undefined behavior
  • You can use a NavMesh Agent to move e.g. a player character, without physics
    • Set players agents avoidance priority to a small number (high priority), to allow the player to brush through crowds
    • Move the player agent using NavMeshAgent.velocity, so that other agents can predict the player movement to avoid the player.

NavMesh Agent and Animator

  • NavMesh Agent and Animator with Root Motion can cause race condition
    • Both components try to move the transform each frame
    • Two possible solutions
  • Information should always flow in one direction
    • Either agent moves the character and animations follows
    • Or animation moves the character based on simulated result
    • Otherwise youll end up having a hard to debug feedback loop
  • Animation follows agent
    • Use the NavMeshAgent.velocity as input to the Animator to roughly match the agents movement to the animations
    • Robust and simple to implement, will result in foot sliding where animations cannot match the velocity
  • Agent follows animation

NavMesh Agent and NavMesh Obstacle

  • Do not mix well!
    • Enabling both will make the agent trying to avoid itself
    • If carving is enabled in addition, the agent tries to constantly remap to the edge of the carved hole, even more erroneous behavior ensues
  • Make sure only one of them are active at any given time
    • Deceased state, you may turn off the agent and turn on the obstacle to force other agents to avoid it
    • Alternatively you can use priorities to make certain agents to be avoided more

NavMesh Obstacle and Physics

  • If you want physics controlled object to affect NavMesh Agents behavior
    • Add NavMesh Obstacle component to the object which agent should be aware of, this allows the avoidance system to reason about the obstacle
  • If a game object has a Rigidbody and a NavMesh Obstacle attached, the obstacles velocity is obtained from the Rigidbody automatically
    • This allows NavMesh Agents to predict and avoid the moving obstacle