Homing Missile in Unity
In this article I will cover how I created a Homing Missile for my Space SHMUP. This Homing Missile is a power up and only becomes available when the player picks up the power up. I covered setting up power ups in previous articles so I am going to skip that and just focus on the functionality of the Homing Missile.
I create a missile sprite and uploaded to Unity. Once uploaded, I adjusted the sizing to fit my game scene, gave it a Tag of “Missile” (this will be important later), added a Rigidbody and Capsule Collider.
Let’s create a script for the Homing Missile that will control the targeting, rotation and movement. The first thing is declaring a target variable that will hold the Transform (position) of our enemy.
In our game Update we will need to find the enemy game object, so we can store it in our target variable. We do this using the Find Game Object With Tag and pass in the enemy tag and get the transform (position) of that enemy game object. At first I did this initialization in the On Start, but discovered if the missile was fired and there was no enemies in Game Scene it would just stay in place. Initializing it in the Update ensures that is constantly looking for the game object with the enemy tag even when, at first there isn’t any, once one shows up it will still be assigned to our target variable.
If our target is not null, we need to find the direction between the missile and the enemy. This will be store in a Vector3 local variable and is the result of subtracting the missile’s position (red arrow) from the target’s position (green arrow).
I believe this is taking a page out of the old math book and using pythagorean theorem (A^2 + B^2 = C^2) to find the hypotenuse of the triangle (the yellow arrow) which is our direction. This needs to be in update because both the enemy and missile will be moving, which means their position will constantly moving resulting in an ever changing direction.
Now that we have the direction the missile has to take to get to the enemy, we want to be able to rotate it so it is facing the target. We can do this by assigning the transform.up to always be moving towards the target. The transform.up represents the top of the sprite (y-axis) and we can use Vector3.MoveTowards declaration.
What this requires is the current Vector3, which is going to be the transform.up (top of the sprite), the target Vector3 (which is being stored in our direction, and the distance you want it to move between each frame. This maxDistanceDelta will be created using a rotate speed variable multiplied by time.deltaTime to get this.
This now we move the top of the missile sprite towards the enemy. But, the whole missile isn’t moving towards the enemy.
In making the whole missile move t0wards the enemy we will use the same line of code as before, but change some of the parameters. We are going to assign the missile’s transform.position to a MoveTowards declaration. A MoveTowards needs a the current position of the missile (transform.position), target position (target.position) and distance to move between the two between each frame (_speed * Time.deltaTime). We will create a speed variable that will determine how fast the missile will move to the target.
We have a working homing Missile at this point, where the missile will rotate towards the target and move towards it at the same time but when it hits the enemy nothing happens. We will have to jump into the Enemy script which handles collisions on the enemy.
In the OnTriggerEnter we will need to check the tag of the object that is colliding with the enemy. Remember at the beginning, we tagged our missile with the tag “Missile”. If the tag is “Missile” when there is a collision we know it is our missile and we can put in all of our destroying enemy and missile code. There you go! We now have a homing missile!