How to make enemy AI in Unity

AI in any context can be fascinating. When it comes to game development AI can be more interesting and challenging. There are many scenarios where you can use AI in game development. Making the enemy follow player is the most used AI functionality in games. In this tutorial, we will see how to bake a navmesh and how to use the navmesh to move the enemy to the player’s location.

What is a navmesh

Navmesh is a class in Unity that stores data regarding paths, objects and meshes that are part of navigation in your game. The data available in the navmesh is used to calculate the path from one position to another while avoiding obstacles.

Enemy follow player infographics

Navmesh Agent

Navmesh agent is the Gameobject that you want to move using AI pathfinding. In our case, the navmesh agent is the enemy Gameobject. To set an object as navmesh agent, select the Gameobject and click add component on the inspector window. Select navmesh agent from the list.

Set the speed, acceleration depending on how fast you want the enemy to move. Stopping distance parameter needs to be set to a value greater than the sun of the radius of both player and enemy. If this is not set properly the enemy AI will keep moving even after reaching near the player.

You will need to optimize these settings based on your game.

Navmesh Obstacles

The objects that will remain static and will act as an obstacle needs to be marked as navmesh obstacle. You can mark a non static object as a obstacle too. But, a non static obstacle will be performance heavy.

The Carve option is used for moving obstacles. When carve is not enables the agent tries to avoid the obstacle but may collide with it when its near. It works similar to a collider in Unity. In case of Carve checked the navmesh data itself is altered and the agent plots a new path to the avoid the obstacle all together.

Baking a Navmesh

Once you have assigned the navmesh agent and the obstacles, the next step is to bake the navmesh.

  1. Go to Windows>AI> Navigation on the Unity editor.
  2. Select all the navmesh obstacles and the plane in which you are going to move the enemy AI.
  3. Do not select the Enemy navmesh agent.
  4. Select the Bake tab on the navigation window.
  5. Click bake. You should see the navmesh marked in blue color on your scene.
baked navmesh

Move the enemy AI

To make the enemy go to the player position you need to set the player transform as the navmesh agent destination. You can find the code sample for the same below.

  1. Attach a script called Playerfollow to the enemy.
  2. Copy paste the code below to the script.
  3. Drag and drop the player Gameobject to the Playerfollow script.
  4. Press play. The enemy will go to the player’s location. If the player moves the enemy will automatically reroute and follow the player.
using UnityEngine;

public class Playerfollow : MonoBehaviour
    public Transform Playerpos;
    UnityEngine.AI.NavMeshAgent agent;
    // Start is called before the first frame update
    void Start()
       agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
    void Update()
          agent.destination = Playerpos.position;    

You can read more about Unity navigation system for understanding the concept completely. Feel free to leave a comment below if you have any questions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.