Setting up our Enemy AI Movement

Jordan Evans
3 min readOct 28, 2021

Now that we have a targeting system in place, we need to start to work on something to actually aim for. Having floating cubes is good target practice and all, but let’s get to work on building AI that moves towards the player. What we will use in a CharacterController based movement system for our enemies, and create a method in which we will have it slowly move at the player.
To start, let’s build in our enemy’s movement method:

What we have done is told our enemy unit that we want the direction to move to be towards the player unit, and to have it’s rotation look towards our player as well, so that when we decide to go towards a figure that is not a plain cube, we will be able to have it look as if it is running at our player. From here, we continue on with the same code that we used on our player unit:

As we can see, there are a couple issues that come from this. To start, our enemy unit snaps to our player, not giving us much of a chance to react to it running. Secondly, once it reaches the player, it will tilt slightly down as if it is looking down on us. To change this, we will need to add in a couple additional lines of code:

With the direction.Normalize(); method, we are limiting how much our enemy unit is able to move within a frame, allowing us to give it a more smooth movement towards our player. By setting our y value to 0, it hard codes the enemy object to stay in it’s upright position:

Now that we have this in place, let’s look at how we are able to create a means in which the enemy doesn’t just go to the player at the start, but need to be within a certain distance to start moving towards the player:

By implementing our distance method, and limiting how close the player needs to be before it starts to move, we are allowing the player some time to figure out what to do with the upcoming threat:

With this, the enemy won’t be activated to chase the player until we reach a certain distance. We can use this method to create some suspense when we can’t see what is waiting around the corner:

Now that we have looked over a few methods to have our enemies move towards the player, it’s time we look at how we can create a universal damage method.

--

--