Let’s build a simple AI follower using Unity’s built-in component NavMesh.
We will use NavMesh Agent and NavMesh Surface for this simple example.
NavMesh Surface — is a component that scans the surface and “bakes” it in a way that our Agent can walk on it.
NavMesh Agent — is a component that you give to your “follower” (any kind of follower, can be an enemy or just a cute minion that follows you around). It knows its height and radius and can go around obstacles.
With our Agent and Surface, we need to make a short script that tells our Agent to move and follow our target, in this case, the Player.
But before that, let’s dive into some NavMesh settings:
In this NavMesh Surface, we are interested in two parameters.
- Agent Type — is Robot, because it knows what kind of agent will walk around on the surface
- Include Layers — is where you specify which GameObject layers the NavMesh Surface will include during the baking process.
This is NavMesh Agent settings, where you can choose Agent Type and give it some basic and pretty straightforward options.
This is Navigation of the Agent. Let’s find out what those options are because I find it very interesting.
- Radius — defines agent’s drum roll radius! We determine how wide our agent is. When baking the mesh, Unity will offset all walls and corners by this amount to prevent your agent from rubbing its sides against textures when walking or getting stuck in narrow passages.
- Height — defines agent’s height. Allows Unity to calculate whether the agent can fit under low obstacles, bridges, or ceilings. If the clearance height is less than 1, the mesh underneath it will not be baked.
- Step Height — determines the maximum height of a stair or threshold of an obstacle that the agent can climb. We want to make sure that this option is always lower than Height.
- Max Slope — the maximum degree of surface slope that the agent is physically capable of walking up or down.
- Drop Height — determines the maximum height from which agent can jump. In our case 9. If the actual height of an object is more than 9 then agent will not drop down.
- Jump Distance — is the maximum horizontal distance that agent can jump.
Now let’s check the code out:
using UnityEngine;
using UnityEngine.AI;
using StarterAssets;
public class Robot : MonoBehaviour
{
private FirstPersonController _player;
private NavMeshAgent _agent;
private void Awake()
{
_agent = GetComponent<NavMeshAgent>();
}
private void Start()
{
_player = FindAnyObjectByType<FirstPersonController>();
}
private void Update()
{
_agent.SetDestination(_player.transform.position);
}
}
To use NavMesh we need to import its namespace, which is UnityEngine.AI. I also use StarterAssets for this project for a premade first-person controller from Asset’s Store.
First, we need to declare and cache our variables. We retrieve the _agent component in Awake() since this script is attached directly to the Robot game object. Then, we find the _player in Start(). In the Update(), we call the built-in SetDestination() method, which takes player's Vector3 position. Once the game starts, the agent will dynamically pursue the player wherever they go.
And that’s it. This is how to make a simple AI follower in Unity.



Top comments (0)