My journey becoming a Unity game developer: Laser prototype for Galaxy Invaders pt.2

Rhett Haynes
4 min readJun 11, 2022

--

Objective: Create an offset position to have the Laser shoot from the Player without touching the Player object. Also, implement a delay between each laser fired to prevent the Player from overloading the screen with lasers using a Coroutine.

Player shooting lasers at a minimum of 0.3 seconds per shot.

We need to create an offset position for the lasers each time the player shoots to prevent the lasers from coming out of the Player’s body, and colliding with the Player object. This can look weird to the user playing the game seeing lasers coming out of their body. Plus, lasers killing the Player itself every time they shoot will make this game unplayable.

To fix this issue, let’s take the Laser prefab and find the position for it slightly above the Player where the Laser and Player objects are not touching. Once we’re satisfied with the position of Laser above the Player, copy the Laser’s Y-axis value as we will use this in our code to position it as the starting point to shoot out from the Player using Instantiation.

Laser adjusted to show where it should instantiate from.

In the PlayerBehavior script, add a new Vector3 offset to the transform position in the Instantiate() method. In the new Vector3 Y-axis position, use the value copied from the Laser’s offset position we settled on which was 0.8f for when the Laser was completely off the screen.

Added an offset to the Laser to instantiate above the Player object when fired.

Playing the game, we see the lasers are instantiating at 0.8f above the Player object, thus avoiding any conflicts between the Laser and Player objects.

Lasers instantiating at 0.8 meters above the Player when fired.

Now we have to fix another problem which is the Player ability to shoot lasers in rapid fire. We’re going to create a time delay between each laser fired.

Player able to shoot lasers in rapid fire.

Back in the PlayerBehavior script, create a new variable Type Bool name canFire. The Access Modifier security level will be set to Private, but I’m going to use the SerializeField attribute for demonstration purposes.

Created a variable name canFire to see if the Player is allowed to shoot.

Then, in the Start() function, set the canFire variable to True as this will allow the Player the ability to shoot a laser at the beginning of gameplay.

Set canFire to True at the beginning of the game.

In the Update() function, right after the Laser is instantiated when the Space Bar key is pressed, we want to set canFire variable to False to prevent the player from shooting again. After that we want to Start a Coroutine for the LaserDelay() IEnumerator that we must create.

Player must wait 0.3 seconds each shot through LaserDelay Coroutine.

Create a Private Type IEnumerator name LaserDelay() to handle the delay between laser shots. First statement inside LaserDelay() will be to pause or Yield, then Return a result of waiting for 0.3 seconds before setting canFire to True again.

LaserDelay coroutine created to wait 0.3 seconds, then turn canFire back on.

Lasers are now shooting at 0.3 seconds per shot from the Player.

Lasers shooting at 0.3 seconds per shot.

We should optimize our code inside the IF statements by selecting all the code, cutting it out, and placing it into a new method name FireLaser(). FireLaser() method will activate when the Space Bar key is pressed & canFire is True.

Refactored the code creating a new method named FireLaser().

Lasers shooting out at 0.3 seconds in front of the Player object with no issues.

Lasers firing using the FireLaser() method.

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet