Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Components of Navmesh
3.
Example
3.1.
Generating the NavMesh
3.2.
Tuning the NavMesh with Layers
3.3.
Removing an Object from the NavMesh
3.4.
Setting up the Player
3.5.
Making Our Player Move
4.
Frequently Asked Questions
4.1.
What is unity3D?
4.2.
What is NavMesh?
4.3.
What is baking in unity?
5.
Conclusion
Last Updated: Mar 27, 2024

Navigation in Navmesh

Author Ayush Tiwari
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Have you ever wondered how the gaming bots decide which path they should choose and which path to avoid? How do the various non playable characters intelligently move around the game world,avoiding objects and players?

This miracle is performed using Navmesh. Navmeshes are the part of the navigation and path finding system of unity. It allows you to create characters that can smartly move around the game world, using navigation meshes that are created from your Scene geometry.

Navigation in Navmesh

Components of Navmesh

Components of Navmesh

The unity Navmesh consists of following components:

Navmesh: It is a data structure that describes the walkable surface of the game world and allows one to find a path from one walkable location to another.

Navmesh Agent: This component helps you create characters which avoid each other while moving towards a goal. Using Navmesh, they know how to avoid each other as well as moving obstacles.

Off-mesh link: This component allows you to incorporate navigation shortcuts like opening a door before walking through it.

Navmesh obstacle: This component allows you to describe moving obstacles the agents should avoid while navigating the world.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example

There is a need for the scene to run around. In order to have one,go to Assets/RW/Scenes and open SampleScene.

Generating the NavMesh

Step 1: Create/make an empty game object and name it NavMesh.

Step 2: Now, add a NavMeshSurface component to this object.

Step 3: Now, Click Bake, and Unity generates a navigation mesh using the default settings.

Generating the NavMesh

source

Tuning the NavMesh with Layers

By default, everything in the scenario is considered a potential obstacle to move around.You need to tell NavMeshSurface not to include the player as an obstacle.

Following are the steps to put the player on a new layer and then exclude that layer from the mesh.

  1. Select the Player GameObject in the Hierarchy.
  2. In the Layer dropdown, select Add Layer.
  3. Type the name Player in the first row in the layer management view that appears,
  4. Again select the player. Use the Layer dropdown and change its layer to the newly created Player layer.
  5. A popup will appear, asking whether to assign child objects to that layer as well. Choose change children.
  6. Select NavMesh.
  7. In the Inspector, open the Include Layers dropdown and click Player to deselect it.
Tuning the NavMesh with Layers

source

Removing an Object from the NavMesh

The player should be able to walk only on the floor and not on top of the walls. But you cannot let it go that block as you did with the player. It needs to be an obstacle so that NavMesh won’t generate paths through it. NavMeshModifier can handle this problem.

  1. Look at the children of Maze and select Corner.
  2. Add a component named NavMeshModifier.
  3. click to enable Override Area on the component.
  4. Change Area Type to Not Walkable.
  5. Click on NavMesh and bake the navigation mesh again.

Setting up the Player

Setting up the player is quite easy. We just have to add the Component “NavMeshAgent” to the Player object.

  • Select the Player object in the Hierarchy.
  • Click on the Add Components drop down in the inspector.
  • From the drop down, select Navigation>NavMeshAgent.
Setting up the Player

source

Making Our Player Move

To order to make our player move, we will attach a c# script to it, which will make the NavMesh move our player.

Attach a C# script named “Controller” to our player object

using UnityEngine;
using UnityEngine.AI;

public class Controller : MonoBehaviour 
{
    public NavMeshAgent playerAgent;

    void Update () 
    {
        if (Input.GetMouseButtonDown (0))
        {
            Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);

            RaycastHit hit;

            if (Physics.Raycast (camRay, out hit))
            {
                playerAgent.SetDestination (hit.point);
            }
        }    
    }
}

And we are done here. You can play with this. Create your own levels and bake the NavMesh.

Frequently Asked Questions

What is unity3D?

Unity3D is a powerful cross-platform 3D engine and a user-friendly development environment.Unity should interest anybody who wants to easily create 3D games and applications for mobile, desktop, the web, and consoles.

What is NavMesh?

Navmeshes are normally used in pathfinding. These are a type of data structure which are designed to find a path between some objects over a large space.

What is baking in unity?

Unity performs the calculations for Baked Lights in the Unity Editor, and saves the results to disk as lighting data. This process is called baking.

Conclusion

In this article,we have discussed how to navigate in a game space using navmesh.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available, interview puzzles, take a look at the interview experiences, and interview bundle for placement preparations.

We hope that this blog has helped you enhance your knowledge regarding puzzles, and if you liked this blog, check other links. Do upvote our blog to help other ninjas grow. Happy Coding!"

Thankyou
Live masterclass