Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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.
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.
Select the Player GameObject in the Hierarchy.
In the Layer dropdown, select Add Layer.
Type the name Player in the first row in the layer management view that appears,
Again select the player. Use the Layer dropdown and change its layer to the newly created Player layer.
A popup will appear, asking whether to assign child objects to that layer as well. Choose change children.
Select NavMesh.
In the Inspector, open the Include Layers dropdown and click Player to deselect it.
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.
Look at the children of Maze and select Corner.
Add a component named NavMeshModifier.
click to enable Override Area on the component.
Change Area Type to Not Walkable.
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.
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);
}
}
}
}
You can also try this code with Online Java Compiler
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.
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!"