Journal 33 — Unity Modifications to Game, Part 1

Chris Nielsen
6 min readMay 18, 2021

--

After completing several steps to finish my first game project, I will try a few more ideas on how to modify the gameplay.

I’ll cover these modifications:

— Add a thruster speed up by holding in the Left Shift button

— Change the shield strength to allow for multiple hits before it’s lost

— Show the player ammo count, and the player can’t fire when out of ammo

— Add a new collectable power up that adds ammo

Speed up by holding in the Left Shift button is fairly straightforward. We can update the player movement, so that when Left Shift is held down, the movement speed is increased temporarily. For this example, I tried an increase of 75%, or 1.75x the current speed.

In the Player C# script, I have added code to check for Input.GetKey, which is used for holding the key pressed.

Unity C# Player script, add Left Shift to speed up

And here are the results. Simply holding down the Left Shift key speeds up the player.

Unity Playmode, player speeds up while holding Left Shift

Another modification to the basic game is to change the shield strength behavior, which for my practice game is handled in the Player C# script. For this modification, I worked on three major updates:

— Reference the player shield, and change its color/intensity

— Add a shield strength variable to allow or three levels of damage

— Add code to make sure the shield strength can be replenished if you collect another power up

To begin, I created an integer to represent three levels of shield strength.

Unity C# script for Player, add tracker for shield strength levels

In the Damage method, I used the shieldstrength variable and the shield’s Sprite Renderer color to add a darkening effect to the shield animation, with RGB values at white varying from 1,1,1, down to black being 0,0,0.

Unity C# script for Player, update Damage method for different colors of shields

During gameplay, we want to make sure that the shields can be replenished at any time a new power up drops, so in the ShieldActive method, I updated the code to reset the shieldstrength value, and also changed the shield color intensity back to full strength.

Unity C# script for Player, update the shield power up function to replenish shields

Here are the results of this implementation. You can see a darkening effect on the shield, before disappearing completely.

3…

2…

1…

Ammo count consists of the following modifications:

—Add new UI to track the ammo count

— Add an integer variable to count the total number of shots fired, starting with 15 and subtracting when hitting the spacebar

— Include conditions that once all 15 shots are used up, it’s clear from the UI or there are sounds that no more shots are left

To start, we can update the UI Manager with an ammo count text game object and text variable, similar to the score count created earlier.

Unity C# script for UIManager, variable for ammo text
Unity C# script for UIManager, update ammo method

Under the Player C# script, we will track the number of laser shots available, and a bool to change if the ammo is empty.

Unity C# script for Player, laser ammo count and bool to check if ammo is empty

In the Player Start method, we can set the bool to false, and our initial pool of laser shots, and update the UI Manager with the ammo count.

Unity C# script for Player, Start method to initialize ammo count, bool, and UIManager

In the Player Update method, we can set the new bool variable as a third condition for when pressing the spacebar. So after pressing the spacebar, we subtract from laser ammo, and update the UI. Once the laser ammo is empty, we change the ammoEmpty bool to true, and you can no longer press the spacebar and fire shots.

Unity C# script for Player, change to firing lasers to disable if ammo is empty

Now that we have figured out that our ammo count works, we can add an ammo power up to replenish stock. In this example, I made a simple icon in GIMP. In order to animate, I made several color changes to the background, turned on the different background colors per image, and exported to a series of PNG files.

GIMP, simple image creation or ammo power up, export to PNG and copy into Unity assets

After dragging and dropping the image files to a new Unity Sprites subfolder, I selected all the images and switched the Texture Type to Sprite (2D and UI) so they can be used in Unity.

Unity change settings for ammo power up images

Now that we have the sprites imported, we can create a new basic animation with the series of PNG images created earlier.

Unity Animation for Ammo power up

The new Ammo power up needs a RigidBody 2D and Box Collider 2D with a trigger set, same as the other power ups.

Unity Inspector for ammo power up

In the SpawnManager C# script, we need to expand the array we originally created for the other three power ups. This can be easily updated in the Inspector, and drag in the new Ammo power up.

Unity C# script for SpawnManager, update powerUps array for new power up

In the Powerup C# script, we can update the powerupID switch statement, and call a method in the Player script to handle the ammo power up.

In the switch statement, we have added reference to the AmmoPowerup method in the Player script. In addition, while testing I found the ammo power up can actually credit two power ups, due to the brief delay in when the power up is destroyed. So I added a command to turn off the Box Collider so it could not be triggered after one time.

Unity C# script for Powerup, add new power up option

In the Player script, we have a new public method that adds 15 to the ammo count, and sets the ammoEmpty bool back to false, and updates the UI.

Unity C# script for Player, method to add box of ammo and update UI

To make sure the power up spawns, don’t forget to update the instantiate call in the SpawnManager script, to match the new range of power ups.

Unity C# script for SpawnManager, add ammo power up to list of random spawns

Finally, here are the results of the power up, and the UI successfully credits a pick up!

Unity Playmode, Ammo power up implemented

Thank you for stopping by.

--

--

Chris Nielsen
Chris Nielsen

Written by Chris Nielsen

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

No responses yet