AI Navigation in Unity help

So I have been following the tutorial series that Filmstorm made using Playmaker

and I just had a few questions that hopefully someone might be able to answer and help me with.

My enemy AI is close to how I want it, when it sees me within a certain distance it runs at me, draws its weapon and attacks, does damage and can die with an animation and everything.
Unfortunately I cannot find a way to give the AI pathfinding. They will chase me up until I get a certain distance away, where they will then stop running and put their weapon away. But when they are chasing me they do not register any objects in their way or anything.
Does anyone know of a way to implement Pathfinding on these guys?

1 Like

Hey @krockhunter,

It’s great to hear from you again :smiley:

Yes, this is super simple to do and can be done quickly - although you’ll need to use C# - eek! But it’s worth using C# as setting it up in playmaker will take 3 times the time. :smiley:

Here’s what we will make:

movement

Are you ready?

Open your project and the first you’re going to want to do is make sure to set your environment objects to static - this is so Unity understands what is going to place in the world and knows it will not move.

Once you’ve marked your environment objects as static go to the navigation pane and click build nav mesh -> this will generate a navmesh layout of your world and you should see a blue outline on the floor.

This is the new walkable area for your NavMeshAgents.

But what are these NavMeshAgent -> they are a component you need to attach your enemy objects which will drive them around the world.

Click add component on your enemy and search for NavMeshAgent and add it.

Great, now we need to write some code to make the agent follow our player object.

Right-click in your Project Browser and create a new C# Script -> call it EnemyFollower.

Double click the new script to open it and edit.

With the script opened you’ll need to enter this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyFollower : MonoBehaviour
{
    NavMeshAgent navMeshAgent;
    [SerializeField] GameObject targetToFollow;

    // Start is called before the first frame update
    void Start()
    {
       navMeshAgent = this.GetComponent<NavMeshAgent>();
        
    }

    // Update is called once per frame
    void Update()
    {
        navMeshAgent.SetDestination(targetToFollow.transform.position);
       
    }
}

This is a super simple script I just wrote in a couple of seconds, basically, we create new variables for the navMeshAgent and the GameObject we want to follow.

In the Start() method we get the navMeshAgent component of the Enemy it’s attached to.

Then in the Update() method we just set the destination of the enemy to the Player’s Transform Position. Done! (You can really make this a lot more complicated, but we’ll keep it simple for the sake of this tutorial.

Now back in Unity drag the Script onto the Enemy object. (I’ve also colored the enemy Red and player Blue.

Drag your player into the new slot that’s appeared, called Target To Follow. In the NavMeshAgent setting change the stopping distance to 2 for testing (this stops overlapping of the enemy with the player).

Press play and you should see the enemy move towards the Player. You can move the player around and the enemy will keep following.

There you go, now you can customize and make the system work for your game.

movement

1 Like

Awesome. Thanks for the quick reply. I’ll mess around with it and see what I can do. Hopefully I can get it to work how I want.

1 Like

Great! If you need help with anything else just let me know :smile:

1 Like

Just checking how you went setting this up, did you need a hand with anything else?

I have things how I want them for the time being while I am still learning everything. I did have a problem when I left the enemies zone which triggers them to stop chasing and putting their weapon away, but the NavMesh Agent would make them slowly slide towards my last standing position when I left their range.
I ended up changing the script a little so that the NavMesh would turn off when I left the range and turn back on if I reentered it. It still messes up sometimes but I’ll cross that bridge when I really start to work on enemy AI.