Geek Culture
Published in

Geek Culture

Phase II: Homing Missile, Part 1

Objective: Our next feature will be a homing missile for the player with a powerup to pick up.

I will have the player hold a maximum of three missiles with each powerup they collect, providing one. Also, I’ll have the player fire a missile by pressing the “x” key.

First, I need to add some new variables to the UIManager script.

Next, in the UIManager, I create a UpdateMissileCount() method to update the missile count on the UI. This also checks if the missile count is below 0 or above 3 and resets to those min or max values.

UpdateMissileCount() method.

That should be it for the UIManager changes. Now I’ll create the missile functionality for the player. First, I add the needed variables.

I am also adding two new methods. One will be MissileCollected() which will update the missile count on the player and call the UpdateMissileCount() method on the UIManager to update the display.

MissileCollected() method.

The other new method is MissileControl(). This method checks if the player presses the “x” key and if the player has missiles. If so, it instantiates a missile at the player’s transform position with a small offset then sets the missile count to zero in case it drops below. Finally, it calls the UpdateMissileCount() method on the UIManager with -1 to reduce the missile count display.

MissileControl() method.

The last thing I need to do is create the powerup and add it to the powerup script. I add another case to the switch statement in the powerup script to call the MissileCollected() method on the player.

Then I create the powerup. I created the below sprite for use with this.

Missile Powerup Sprite

So I pull the sprite into the hierarchy and then back down into the prefab folder to create a prefab. After that, I add a CircleCollider2D, a RigidBody2D, and the Powerup script to the prefab and make sure all of the settings correct through the inspector.

This covers setting up the powerup, updating the display, and having the player pick up the powerup and fire the missile. All that is left is to create the missile and program it to home in on the nearest enemy. I will cover that in my next article.

So until then, I hope you found this interesting, and I wish you well on your coding journey.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store