Navigation using NavMesh Agent: Unity Tutorial

In this article, we will see how to implement navigation using NavMesh agent in unity3d. NavMesh agent is useful when you need to set navigation path of any GameObject to reach a destination point. The destination point can be set dynamically at runtime.

Implementation of NavMesh Agent

  • Create a new project in unity. Prepare the scene as given below.
    Navigation using NavMesh Agent Scene
    Details of the added GameObjects in the scene:

    • Ground (Plane): Plane on which all other GameObject will reside. This will be the walkable area.
    • Obstacles: This is an empty GameObject. The child of this GameObject will be Obstacles. These objects will be non-walkable. Mesh Renderer component should be attached to each obstacle GameObjects.
    • Destination (Cube-Yellow Color): Destination GameObject which will decide the end point.
    • Player (Cube-Green Color): This GameObject will navigate in the scene and will reach to the Destination GameObject.
  • Select Player GameObject in the hierarchy. Add NavMesh Agent component in the inspector.
  • Open Navigation panel (Window -> Navigation).

    It will open navigation panel as given below (No GameObject is selected in the scene hierarchy).By default, Object tab will be selected.
  • Select all the child of Obstacles and Ground GameObject. Mark them as static objects. Mesh Renderer components should be attached to these GameObjects.
  • Now select the obstacles in the scene (All the child of the Obstacles GameObject) and make it non-walkable in Navigation Panel. By default, every GameObject will be walkable.

    The ground should be walkable.
  • Select Bake tab in the Navigation Panel. Click on Bake button to generate NavMesh Path. Clear button will delete already generated (if any) path.
    You can set below bake settings otherwise leave it as default.

    • Agent Radius: Decides how close the agent center can go near the non-walkable objects.
    • Agent Height: Decides height of the agent.
    • Max Slope: Ramp slope that agent walks up
    • Step Height: Decides the height of the obstacles that agent can step up
  • After clicking on Bake button, it will generate a NavMesh Path in the scene. The non-walkable GameObjects will not be included in the path.
    The path will be stored in Scene folder as NavMesh (Assets -> Scene -> NavMesh).
  • Create a new script and attach it to the Player GameObject.
  • Run the application. The player GameObject will be navigated to the destination GameObject with the help of NavMesh Agent.

Hope you get an idea about how to implement NavMesh Agent in unity3d. Please post your comments for queries and feedback. Thanks for reading.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Leave a Reply

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