Journal 34 — Unity Modifications to Game, Part 2

This article is about the next round of updates to enhance the gameplay.

I’ll cover these modifications:

— Add a collectible that gives back health

— Add a secondary fire power up (special power up)

— Add a scale bar to show the use of the thrusters with a cool down notice if used too long

— Add a camera shake effect if the player takes damage

Health Collectible

For this modification, you can use one of the images from the UI and modify it to move down the screen like other power up game objects.

Unity Playmode, health power up

The health power up is added to the Powerup C# script in the expanded switch statement. Also be sure to add the health game object to the SpawnManager powerup function and drag the prefab into the Inspector.

Unity C# script for SpawnManager, add extra life power up to switch statement

In the Player C# script, if the health power up is picked up, we can add back a life to the tracker, update the UI display for ship lives, and also turn off the damaged ship thrusters.

Unity C# script for Player, method for fixing health upon collecting extra life power up

Secondary Fire Power Up

In this modification, we can add a secondary fire powerup that spawns a little more rare than other power ups. As you see from the animation below, this power up unleashes a spiral of lasers for a few seconds to try and clear all enemies.

Unity Playmode, special secondary fire

To make this spawn slightly more rare than other power ups, you can add a check for random numbers from say 0 through 19, and if the results fall below 15, then call a random power up that does not include the special power up. However if the random number is greater than 15, then that calls the special power up.

Unity C# script for SpawnManager, random number generation to spawn different power ups

Update the Powerup C# script with an expanded switch statement.

Unity C# script for Powerup, added secondary fire special power up

In the Player C# script the laser shot special power up, we can make a loop to create an array of 50 laser prefabs on the player, they are instantiated at the player’s position, and each laser prefab is given a 30 degree rotation around the z-axis times the position of each prefab in the array. This creates the spiral around effect. Since this is done in a coroutine, we pause for about 0.15 seconds per laser projectile.

Unity C# script for Player, instantiate laser prefabs in array and send out at timed interval

Ship Thrusters Display and Cooldown

In this modification, we want to turn on and off the ship’s thrusters when holding in the Left Shift button for maximum amount of time, and then the thrusters have a cooldown period and can’t be used.

Unity Playmode, thrusters turned on and cooldown

The UI update to show the thruster usage is based on a radial bar. You can make a basic circle in any art program, and copy it into the Unity project folder and change to a sprite. See the settings below.

Unity Inspector for thrust indicator, settings

In the UIManager C# script, we add the following variables:

Unity C# script for UIManager, thruster image, text, and timers and keycode

In the Player C# script, we add one more condition to when using the thrusters, plus we show visually the thrusters when they can be used.

Unity C# script for Player, update conditions for thruster use

In the Player C# script, we add two public methods that contain a bool variable that will be accessed in the UIManager script.

Unity C# script for Player, bool functions to set when thrusters can be used

The last edits for the radial bar is in the UIManager C# script. As described below, we have a timer for how long the thrusters have been activated in comparison to the maximum allowed timer. If the thrusters are used too long, we reference the bool variable in the Player script to disable the thrusters, and show the cooldown text.

Unity C# script for UIManager, radial bar display and timers for tracking cooldown
Unity C# script for UIManager (cont’d), radial bar display and timers

2D Camera Shake

The last modification for this article adds a camera shake effect if the player is damaged.

Unity Playmode, show camera shake on damage

We can create a new C# script and add it to the camera. The script will track the camera’s position and make a random movement of the camera’s position based on a set duration of shaking.

Unity C# script for CameraShake, variables and set up

The TriggerShake() method is made public, so we can access this from the Player script when damage is taken.

Unity C# script for CameraShake, Update method and trigger function
Unity C# script for Player to access main camera
Unity C# script for Player, when damaged, access TriggerShake function

Thank you for your time!



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