Stuck on you.

Objective: Adding in homing missiles as an extra fire mode.

Rose Owen
6 min readJun 19, 2021

Time to add another firing method, this time with missiles!

As with most things, there are many ways to do the same task. This is just one way and as you can see it works well.

Making the Prefab:

Find or make an Image for the missile, After we have that we can add it to the project. You will need to change the texture type to Sprite (2D and UI).

Now we can start making the Prefab. Drag the image into the hierarchy and add a rigid body 2D, a Box Collider 2D, and change its tag to Laser (this will make things easier later on)

Next, add an empty game object to the hierarchy, this will be used to contain all the objects for the homing missile. Add a circle collider and make the radius a lot larger than the missile, this will be used to detect the enemy. We can also make a new script and attach it, we will use this to detect the enemy and then shoot towards the enemy.

Leave this one untagged.

Lastly, we can add in the thruster, I ended up reusing the one from the player. Duplicate one of them and move it to the be the child of the empty game object we just made.

After that, we can move it into the prefab folder and start on the scripting for the homing missile.

Scripting:

Open the script and now we can add in some variables.

  • A Bool. This is for if the enemy is found.
  • A Transform. To hold the transform of the enemy when it’s found.
  • A GameObject. This will hold the missile, an easy way to be able to destroy the missile.
  • And Another GameObject. This will be to hold the thrusters.

Now in the update method, we can add in some functionality.

  1. Look for the enemy and if the enemy isn’t found just fly forward slowly. We can also set the thrusters to a smaller size to make it seem like it’s flying slower.
  2. If the enemy is found we can follow the enemy and set the thrusters bigger to make it look like it’s traveling faster.
  3. If the missile makes it outside a set size, destroy the missile.
  4. This one will make sense later but if the missile is null, destroy this gameObject.

Let’s start on the thrusters first, since it’s the easiest part. All that is happening here is more or less what we were doing with the thrusters on the player. When the Method is called, it changes the local scale and the position of the thrusters to line up with the rocket.

With the bigger collider on the Empty holding the missile, we can use it to be able to get the enemy and its transform. After that, we can use that transform to move towards it.

Here we can use the OnTriggerEnter2D and check to see if what we collide with is the enemy, If an enemy hasn’t been found we can change it to found and use the enemy transform we made before and assign it to what we collided with’s transform.

All that is left for the setup is to use the enemy transform and head towards it till we connect and the enemy gets destroyed.

We can pass in the enemy transform that we got from the OnTriggerEnter2D in the last part. Next, check if the enemy is null and if it is, change the bool for if the enemy is found to false. If it isn’t null, we can make a new vector3 for the direction towards the enemy. To work out the direction we can take the enemy transform and take away our own position.
Vector3 Direction = EnemyTransform.position - transform.position;

Calculate and rotate the missile towards the enemy. We can do this by using mathf.Atan2 using the directions y then x value (this returns an angle in radian) then multiply by mathf.Rad2Deg (which changes the radian back to a degree we can use). Now we need to change that to a quaternion, making a new quaternion naming it something simple q will do. Using Quaternion.AngleAxis(angle, Vector3.forward); which creates a rotation which rotates angle degrees around axis which in this case is Vector3.forward . Next, we can change the transform of the missile using a Lerp, which will smoothly move from the current rotation to the rotation that faces the enemy. transform.rotation = Quaternion.Lerp(transform.rotation, q, Time.deltaTime * 2);

Last is to use the direction we got and use that to head towards the enemy. First, we will want to normalize the vector, then use transform translate using the direction. If it’s not moving fast enough/too fast, we can use direction * speed * Time.deltaTime this will give more control over the speed at which it moves.

Now that everything is set up, we can now add in the ability to shoot the rockets from the player. First, we need to set up some variables.

  • One to hold to the homing missile,
  • One to limit being able to fire off the missiles,
  • One to store the number of missiles we have.

Next, we can check to see if the E key is pressed, if we are able to fire the missiles, and lastly if we have any missiles left.

Lastly, we want to reset the timer for how fast we can fire missiles, then we want to Instantiate the homing missile prefab we made, take a missile away from the count and update the UI (will go into more detail on that one in the next one).

This was another long one, let’s add the pickup and the UI for the rockets in the next one. As always, remember to test things out and see you in the next one!

--

--