It’s great to hear from you again
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.
Here’s what we will make:
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:
public class EnemyFollower : MonoBehaviour
[SerializeField] GameObject targetToFollow;
// Start is called before the first frame update
navMeshAgent = this.GetComponent<NavMeshAgent>();
// Update is called once per frame
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.