My journey becoming a Unity game developer: Phase-46

Rhett Haynes
6 min readAug 10, 2021

Objective: We will use an onscreen element and sound effects to indicate the player is out of lasers. Plus, add a laser powerup that will refill the player’s laser amount to the maximum of 15.

The final result of the Laser Powerup.

In the Player script, we will create variables that will limit the player’s amount of lasers with a maximum and a minimum amount. Since we already have a variable that gives the player 15 lasers to start off with, we will use this variable to set the player’s limit soon. Also, create a variable that will hold the audio clip which will play when the player is out of ammunition.

Inside the FireLaser() method, let’s use the laserFire variable to keep the player constrained to 15 lasers the most and zero the least with the Mathf.Clamp() function. This function uses FLOAT as its TYPE when working with values. Therefore, we must use EXPLICIT TYPE conversion to use the INT TYPE variables we created by using a CAST OPERATOR. Use the laserFire variable and set the values with the CAST TYPE (int) in front of each value. This will now convert each value and result into INTEGERS, instead of the preset FLOAT. Now when the IF conditions are executed, no matter if the Triple Shot or regular shots are used, the laser’s amount will be limited between 0 and 15 as whole numbers.

Also, we will gain access to the UIManager script using the uiManager variable to update the amount of lasers the player has with a method name LaserUpdate() and passing in the laserFire variable itself.

Mathf.Clamp() used to control the limits of lasers the player will have. LaserUpdate() method used update the player’s laser amount.

Before we head over to the UIManager script, let’s turn on the noAmmoClip when the player has no more lasers in the FireLaser() method. Use an IF condition to check if the laser amount is zero. If so, we want to immediately stop shooting using the StopShooting() method. Then we will stop the laser sound from playing using AudioSource.Stop() function through the handle variable audioSource. Using the AudioSource component, change from the laserSoundClip to the noAmmoClip. We will have the sound play over and over by turning on the LOOP setting. Now we will play the clip using AudioSource.Play(). Last if the player picks up new lasers, we will turn off the noAmmoClip loop sound and switch back to the laserSoundClip variable to play the laser’s sound again.

Zero lasers will play the noAmmoClip sound effect. Player with lasers will hear the laserSoundClip.

One more thing we need to do before heading over to the UIManager script is to create a game object name Ammo_Text to hold the actual text representing the laser amount onscreen through the Canvas. Position it wherever you like onscreen by using Anchor button, change the color of the text so you can see it onscreen, change the font size to the size you would like onscreen, and set the text alignment inside the text field using the Paragraph setting Alignment. If you need to adjust the width or length of the Ammo_Text field to see the text onscreen properly, use the Rect tool.

Ammo_Text game object created to show the laser amount onscreen.

Over in the UIManager script, create a variable that will show how many lasers the player has onscreen. Inside the Start() function, set the laserText variable in the Canvas text component to the actual text you will see onscreen which in this case will be (“LASERS: “ + 15). Create the LaserUpdate() method that will be used in the Player script when we pass the actual lasers the player has to it. Inside the method, set the laserText object to show what will be onscreen which is (“LASERS: “ + the laser amount being passed into the method and convert it to a STRING().

LaserUpdate() created to show the amount of lasers onscreen using the laserText variable with the text component.

Finally, let’s create a Laser powerup to reload the player’s ammunition amount to its maximum of 15. In the Player script, create a variable name isLaserBoostActive to see if the player has the powerup and set it to FALSE.

Create a method name LaserBoostActive() which will activate the powerup when the player grabs it. In this method, we will set isLaserBoostActive to TRUE to activate the powerup for the player. The player will also be able to shoot again after grabbing the powerup. The player’s laser will be refilled to the maximum of 15. We also want the lasers amount updated onscreen by using the UIManager script to access the method LaserUpdate() and pass in the lasers amount the player has currently. Turn off the noAmmoClip sound effect by using the AudioSource.Stop() function. Last, start the LaserBoostPowerDown coroutine to give the player 0.5 seconds before deactivating the powerup by setting the isLaserBoostActive variable to FALSE.

On the PowerUp script, down in the OnTriggerEnter2D() function add a CASE 3 statement representing the powerup ID number for the LaserBoostActive powerup.

LaserBoostActive() method added to represent powerup ID# 3.

In the Inspector, drag a sprite that will be the Laser_Powerup into the Hierarchy to be the game object. Rename the sprite, change its color if there are other powerup sprites that look the same. Add a 2D collider and turn on isTrigger to collide with the powerup. Also, add a Rigidbody2D and have Gravity Scale=0 to take gravity off the powerup. Add the PowerUp script component, assign the number 3 into the Powerup ID slot representing the Laser Powerup. Then add the audio clip that will play when the player grabs the powerup into the Powerup Clip variable’s slot.

To make the Laser Powerup animate, click on the Laser_Powerup game object and click on the Animation tab. Click the create button and add the new animation to your Animations folder. Click record and drag all the powerup sprites onto the dopesheet. When done click on Overrides to check if any updates need to be applied. Afterward, drag the Laser_Powerup game object into the Assets or Prefabs folder to make it officially a prefab and delete it from the Hierarchy.

On the Player, add the audio clip that will play when the player has no lasers in the Script component variable’s slot.

Assigned audio clips and Laser powerup ID# on script component of Player and Laser Powerup.
Final result of the Laser Powerup.

--

--