Enemy controller for AI

Josh Watts
2 min readNov 24, 2021

--

To make the enemy chase us lets create a new C# script called Enemy AI and attach to our enemy object. Start by creating some variables for our enemy character controller.

Than in our update method we can make the AI move to us by getting the distance to our target (player) and the enemy position.

While this looks like it would work it creates a problem, lets see what happens.

The enemy(the red block) instantly grabs onto us, this is because the entire directional length is being met in one second. What needs to happen is we need to cut the length to one. To do this we need to normalize the vector. With this we keep the same direction but the length is one.

This is a big improvement but there is no rotation how can we do that? Actually we only need only one line of code when we check if our enemy is grounded.

Almost there! The enemy rotates to face us but is tilting, and the reason is that we have not told the code otherwise, so lets do that. We can say that whenever we are grounded that are direction on the y will always = 0.

Now everything works.

--

--

Josh Watts

Hello I am an aspiring game developer learning to develop great games. Right now I am going through a program with GameDevHQ. My favorite game is Half-Life.