Journal 33 — Unity Modifications to Game, Part 1
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.
And here are the results. Simply holding down the Left Shift key speeds up the player.
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.
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.
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.
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.
Under the Player C# script, we will track the number of laser shots available, and a bool to change if the 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.
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.
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.
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.
Now that we have the sprites imported, we can create a new basic animation with the series of PNG images created earlier.
The new Ammo power up needs a RigidBody 2D and Box Collider 2D with a trigger set, same as the other power ups.
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.
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.
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.
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.
Finally, here are the results of the power up, and the UI successfully credits a pick up!
Thank you for stopping by.