Journal 46 — Unity Player Power Up with a Homing Missile!

In this challenge, the player can pick up a homing missile to target the nearest enemy.

This update requires the following:

  1. New artwork, animation, and power up for a homing missile
  2. New homing missile game object
  3. Homing missile script to target the nearest enemy
  4. Update the Spawn Manager to spawn this new power up
  5. Update the enemy behavior for when targeted with shields
  6. Update the UI to display the missiles

For this challenge, I created my own simple artwork and animation in Blender and GIMP.

Blender and GIMP models and animation for homing missile

The missile prefab and the missile power up prefab consist of the following components.

Unity Inspector components for homing missile and power up

For the homing missile script, we start with a few variables for the player, target, speed, and change in angle speed. In the Start method, we add a reference to the player, start a coroutine to destroy the missile after a few seconds, and get a reference to the missile rigidbody for movement.

Unity C# script for homing missile

For the movement of the homing missile to target, we want to use FixedUpdate instead of Update for physics movement.

Unity C# script for homing missile (cont’d)

The last part of the missile script consists of a coroutine to destroy the missile prefab after a few seconds, and a function to find the nearest game object target.

Unity C# script for homing missile (last part)

Over in the Powerup script, we add the new missile power up to call to the player to add the missile power up to its weapons.

Unity C# script for power up

In the Spawn Manager script, we can add the missile prefab to instantiate with a small percent chance.

Unity C# script for Spawn Manager, homing missile instantiate

In the Enemy base script, we add an update to the switch statement for the OnTrigger events, with two conditions for when the enemy has a shield or not.

Unity C# script for enemy, add behavior for when colliding with missile

In the Shield script, we need to add a tag for the homing missile to turn off the shields if struck by a missile.

Unity C# script for shields, turn off shields when hit by missile like lasers

In the Player script, we add the missile power up function to add missiles to inventory, check that the missile count cannot be greater than max storage, and also update the UI. We also need the same script to find the closest enemy as in the homing missile.

Unity C# script for player, missile power up and finding the nearest enemy

In the Player’s Update method, we check for input to instantiate the missile prefab, subtract from inventory, and also update the UI.

Unity C# script for player, update weapons option for firing missiles

For the UI updates, we can follow the same approach as the ship lives. We need a UI image for the missiles and an array of sprites to swap out the missile images as they are used up or added.

Unity UI updates for missiles

The UI update function changes out the image for the missiles in inventory, and also shows and hides the hotkey “v” for firing the missiles when there are some in inventory.

Unity C# script for UIManager, update missile info on screen

Finally, you can see the results in action!

Unity Playmode with missiles

Thank you for your time!

--

--

--

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Belajar Android Developer Fundamentals (Version 2) Cont… (4)

Parallel Mandelbrot Set Using Golang

Picking the right testing tools to achieve DevOps in Jira

What happens when you type ls *.c

10 Ways to Speed Up Your WordPress Site

(Frontend_1) Basic Explanation

An Ode to Second Chances

Multipass with Hyper-V on Windows 10 Home

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
Chris Nielsen

Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

More from Medium

Journal 151 — Unity Project Development, 2.5D Shooter

Creating an Office Den in Unity Part 2: Lights, Cookies and Emissions

Making a RTS game #45: Importing models & animating our characters 2/2 (Unity/C#)

Framing Transposer Virtual Camera Explained in Unity’s Cinemachine