Adding in the visuals for the missiles.

Objective: Adding in the UI and Pickup for the missiles.

Rose Owen
5 min readJun 20, 2021
There is a little trick at play here, read on to find out.

In the last one, we worked on adding homing missiles and the ability to be able to fire them and have them lock on to the enemy. Can be found here.

Pickup:

Let’s start with adding the Pickup.

PreFab:

We can start by adding an empty object, this will hold all the objects for the pickup. Then we can drag in a missile image into the hierarchy, we can duplicate this image 2 more times after it’s all set up so we only have to make 1. On the missile image add the powerup script (set the speed to 0, spinning speed to 0, the ID to 5, and drop in the Powerup Clip), a box collider 2D (Set Is Trigger), and a rigid body 2D(gravity scale to 0).

After one is made you can duplicate it 2x and change the Z rotations to make it spread out.

The image I’m using starts at Horizontal

After doing that we can parent them to the empty from before.

On the empty, add the powerup script (setting the speed to 3, spinning to 2, ID to something not being used (I used 999), and drop in the powerup clip). Make a new script calling it Missile Pickup and then add to the empty.

And with that, the pickup is all set up now to head over to the scripting.

Scripts:

Going to the player script, we can add a public method that we can call when we get the pickup. Since we don’t want to be able to have more than 3 missiles, we can check if we have less than 3 for a start. If there is less than 3, we can add 1 missile and then update the UI (will do this part in a little bit).

Players Script is all done.

Next, we can change the Powerup Script. All that is needed is to add in case 5 for picking up the pickup.

Powerup Script is all done.

Now the one with a bit of substance. Open up the script we just made before, MissilePickup, this will do some magic behind the scenes. Let’s start by adding in some variables, 2 GameObjects which will hold the other missiles for later, and an int to store a random number.

Since we want all this code to run when the pickup comes into the game we can use the Update or Awake methods. First, we want to get a random number and assign it to the int.

Now to add a Switch statement for the results of the random number. Each number is how many missiles we want the player to get. We can do this by using SetActive true and false on the GameObjects.

On case 2, I added in the chance that it could be one side or the other that shows up.

All that is left to do is drop in the GameObjects into the inspector and save the empty as a prefab.

And with that, you should have a working pickup that will spawn from the spawner and have a chance to be 1–3 missiles. Since the powerup script is on each missile, when the player picks up the pickup for the missiles, the ones that are visible will call the Missile Payload on the player.

UI:

This one should be quick. Add an image and text component, changing the image to the missile and the text to 3/3. This will make things easier to line up.

Next, open up the UI manager Script and make a variable to store the Text for the missile count.

Then we can add a public method to call to update the text using the incoming Missile Count as an int.

Next, we can drag the text component to the UI Manager Script.

And lastly, we will need to call the update missiles from the player.

If you hadn’t already from the other images.

And with that, a new feature is fully added. As always, test it out to see that everything is working and I’ll see you in the next one!

--

--