Simple Enemy Follow Behaviour Without Using NavMesh

Ali Emre Onur
3 min readJun 2, 2021

--

This article will be focused on creating a simple Enemy follow behaviour without using Unity’s NavMesh system. To begin with the Zombies, I have created a sample cube for the Enemy. We will want the zombies to follow and attack the player in the game.

By simply subtracting a vector from another, the length of the resulted vector will be simply the amount of difference. This will result in instant movement of the Enemy towards the player.

Luckily, Unity has a ready method to fix this. By using Vector3.Normalize, we simply tell Unity to change the length of the Vector to 1. Check here for Unity Manual.

So if we normalize the Vector:

We get a smooth result:

Still, the enemy is not rotating and simply moving to the player directly irrelevant of its rotation. Of course, we would not want Zombies chasing us backwards — it would not make any sense, does it?-

I have added a minicube as the glasses of the enemy to have a better visual of the direction it is facing.

We are on it’s back, just shoot him!

I was surprised to find out that there was a simple method for this: Quaternion.LookRotation. Click here for Unity Manual. This method simply tells the direction of the local forward for the game object.

And it works, but with a bug! If you watch the video below, you might observe that the enemy instantly turns its head backwards.

Sorry for delay at the beginning

This is the result of Quaternion.LookRotation method as it tells the game object directly to the direction that it is moving. The fix is quite simple. Using Quaternion.Slerp, we can have a smooth rotation.

Quaternion.Slerp (click here for Unity Manual) simply takes in 3 parameters. The beginning of rotation (which is the current rotation of the game object), the target rotation (which is the direction of the player) and the time to complete the turn — which is actually the speed.

Much better!

--

--

No responses yet