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 the 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 in Unity?

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.

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 and acceleration depending on how fast you want the enemy to move. The 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 need to be marked as Navmesh obstacle. You can mark a non-static object as an obstacle too. But a non-static obstacle will be performance heavy.

The Carve option is used for moving obstacles. When Carve is not enabled the agent tries to avoid the obstacle but may collide with it when it’s nearby. It works similar to a collider in Unity. In case Carve is checked, the Navmesh data itself is altered and the agent plots a new path to avoid obstacles 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 the plane and set it as navigation static.
  3. Select all the Navmesh obstacles and the plane in which you are going to move the enemy AI.
  4. Do not select the Enemy Navmesh agent.
  5. Select the Bake tab on the navigation window.
  6. Click bake. You should see the Navmesh marked in blue 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 and 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 to understand the concept completely. Feel free to leave a comment below if you have any questions. In our next tutorial, we will see how to make the player jump from one Navmesh to another using off mesh links in Unity.

Leave a Reply

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

Is game development recession proof? Can you make a living with esports? Asset optimization techniques in Unity Why is Unity widely used than Unreal? Game developer Salary around the World.