Point & click to move and working with Nav mesh for smart AI in Unity

Hernando Nieto Jaramillo
3 min readAug 4, 2021

For making a character to move towards a direction pointed out with a click, it is required to have a navigation ground.

For this project, we will select a floor collider and add a Navigation component by selecting Windows/AI/Navigation

  • Select the Bake tab and press the Bake button at bottom-right

The blue surface is the baked floor. It represents the places the character (player) and an AI can walk on

For testing, create a capsule, place it in Darren’s starting position, rename it and tag it as “Player”. Add a Nav Mesh Agent component.

That way it will only move on the Nav Mesh surface

  • Create a new script, call it Player and attach it to the player game object
  • To move the player towards a position pointed out with a click, we will need to check if it was a left click. If so, cast a ray from that position.
  • After a ray is created, we can access the info about the game object that has been hit by first, creating a variable of type RayCastHit that can be called hitInfo and later checking out if there was any game object hit with Physics.Raycast boolean

To avoid the capsule to pass through the boxes

  • Select the objects game object (which contains all the boxes) and mark it as static
  • Open the navigation tab, click on Clear and later on Bake

Before

After

--

--