Creating Enemy AI in Unity

Matthew Clark
Nerd For Tech
Published in
2 min readSep 1, 2021

--

I am working on a 3rd person zombie survival game. I have the player set up and now it is time to work on the enemies. I started by creating the chasing AI.

I started by creating a basic enemy with a cube. The enemy will be using a character controller so I added that and deleted the box collider. I also created the enemy script and attached it to the enemy.

To get the enemy moving I needed a reference to the character controller.

I would also need a reference to the player.

Similar to the player's movement, I get the direction to move and multiply that by a speed variable. The direction to move in this case is the distance between the player and the enemy. The enemy will also need to calculate gravity for itself.

If the enemy is not grounded gravity will be applied. We will then set the y value…

--

--