Space Shooter Challenge: Secondary Fire Pt 2 — Creating a Missile
The Super Shot laser was pretty easy to implement, so I wanted an additional challenge of creating “heat seeking” missiles.
I found some missile packs within Filebase, and liked the visual look of the one pictured above. There was one problem though, it was a 3D object, therefore it wasn’t going to function well with the rest of the objects in my game.
To solve this, I downloaded Blender, and imported the FBX. model in. Did a quick search on how to attach the materials as I’m not an artist, and ended up with a replicated model of what I had imported to Unity from Filebase.
Then it was a simple render with a transparent background, then add 5 degrees to the rotation before rendering again, repeating until I had a looping set of sprites.
Now I could add it back to Unity as a 2D animated sprite, and make it a prefab. I also copied the Thruster over from the Player and resized it. The enabling and animating of both the missile and the thruster will be covered later when the code functionality is implemented.
I also reused the Powerup sprites as I have previously, recoloring and renaming them in GIMP.
When collecting the Missile Powerup I wanted them to visually appear on the Player to show the User they’re active as opposed to using UI.
On the Player, I created an empty game object called Missiles, then created two empty game objects as children called LeftMissileHolder and RightMissileHolder. These two objects were placed on the wings, as they would be used to Instantiate the Missile Prefabs.
With this in place, I could create some functionality for the Powerup.
First I needed to create some variables, I created a GameObject Array to hold the two Holder game objects, and a GameObject variable to hold the Missile Prefab.
Now I could create a public method for the Powerup to call in the Switch Statement for Powerup IDs.
In this method, I use a foreach loop to access each Holder GameObject in the Array. For each of these, I Instantiate the Missile, at the position of the Holder, and assign it to a GameObject variable so I can then access the parent of the Instantiated missile and assign it to be the Holder.
This allows for both a clean Hierarchy, but also means the missile objects will stay attached to the wings of the Player as it moves around the screen.
Now when the Missile Powerup is picked up, 2 missiles appear attached to the wings of the Player ship. I now need functionality to fire them.
Instead of cluttering my Update method to check which weapon should be used when the Spacebar is pressed, I created a new private method to be called in the existing if statement.
In the WeaponSelect method I needed a way to check for missiles, there were many ways I could do this, but when it comes to firing the missiles, I wanted to be able to select a random one.
I created a New GameObject List to store the Instantiated missiles, and then back in the MissilesActive method added the missiles to the List.
Now in the WeaponSelect method I could check to see if the List contains any objects, and if so, would call the FireMissile method I was about to create, otherwise, call the existing FireLaser method.
For the FireMissile method I first needed to reuse the first line of the FireLaser method and reset the _canFire variable to create a cooldown between firing the missiles.
I then went ahead and created a Missile script to later create functionality for the Missiles and attached it to the prefab. Now I could access the Script using GetComponent, but first I wanted to select a random missile. This meant I needed to access a random index of the list, and assign it to a GameObject variable.
After getting the Script I needed to remove the missile from the List so it couldn’t be accessed again, this would simply leave one object in the List, and when calling the FireMissile method again, the random index would only be able to return the last available missile.
Lastly I could call the Fire method of the Missile script, which would be where I start all the tracking and movement functionality of the missile.
For now, I simply created the Fire public method on the Missile Script, and had it call a Movement method which simply copies the Laser functionality and has it move upwards.
This creates an issue though, this method is only called once, so the Missile doesn’t actually move very far, instead, I used the Fire method to set a MissileActive bool to true. Then in the Update method, I can check for the bool, and if the missile is active, the Movement method can then be called.c
I now had functioning Missiles, but I wanted to expand on them further and have them chase enemies like they were “heat seeking’. Check back tomorrow for an article tackling this!