Objective: Create a new enemy that launches missiles at the player.

It’s time to give the Enemy forces another boon in their arsenal. Homing missiles are a nice addition and add a new level of difficulty to the game.

To Begin we need to create a new enemy and a projectile. We’ve already covered this in a previous tutorial. With those objects created we need to prefab both and create corresponding scripts.

Luckily, we can borrow most of the code from the old enemy. Mainly need to update the code dealing with shooting.

Before
After

Now we need to migrate to the script for the missile. We need to make a list of variables. Speed, game object, animator, float for minimal distance, float for angle changing speed, and a vector3 for the current position.

Make a method for the missile to move towards the player. This method will take the missile and it will track the player in the field of play and chase the player down.

We want to make a coroutine for our missile to self-destruct after a set amount of time.

Next, we make a method to find the player. This will do the quick math to find the player’s position.

We need to set up collision for the missile, if it hits the player, and I want the player to be able to shoot the missiles, so we need to set a collision for the laser too.

This is on the Laser script

We need to call the method that controls the missile moving towards the player on the update method.

In the start method we want to set the target to the find player method and start the coroutine for the self-destruct.

This is all there is to it. We have set a bare bone homing missile system for the enemy. We can now go in a set a sprite for the missile and add animation for the explosion on impact. Don’t be afraid to experiment with this code and I’ll see you in the next tutorial.