… and guns, lots of guns.

Adding the Spray Shot

Christopher West
5 min readJul 1, 2022
Photo by Dan LeFebvre on Unsplash

Welcome back! I’ll be continuing to implement the requirements for the first phase of the 2D games certification in the GameDevHQ Professional Unity Developer Program! Todays requirement is to create a new collectible object for the player that will allow them to obtain a new type of secondary weapon. The requirement reads as follows:

Tasks:

  • Create a new form of projectile. You should already have a triple shot. Include something new from multi direction shot, to heat seeking shots, etc.
  • Replaces the standard fire for 5 seconds.
  • Spawns rarely

Create the collectible

At this point I have created collectibles for 5 different types of powerups. The collectible for this new spray shot is created in the same way as the previous ones so I will only go over the high level steps here. These steps have been detailed in previous articles and don’t bear a detailed restatement here. To create the new collectible I generate new sprite images in photoshop and import them into unity, add a new entry into the PowerupType enum, create a new GameObject/animation from them, attach colliders and rigid bodies, and attach the powerup script. Prefab the collectible into the appropriate folder for the dynamic powerup population code in the spawn manager to pick it up and I have my collectible!

Create the new shot

Much like when I created the Triple Shot, I need to create a new shot and prefab it to be passed into the player class for instantiation when the collectible is picked up. In this instance I created the new shot by duplicating the triple shot prefab and renaming the copy to spray shot. I then reset all of the transforms of the laser prefabs and tweaked the angles of each to be 10 degrees off center. I also added 2 more lasers. The idea is to have 5 lasers emit from the player in a fan configuration spanning 20 degrees to either side of center. once the lasers are rotated and positioned I was faced with another issue. I need these lasers to move at an angle not just up or down!

Adding trajectory angle to the laser script

To accommodate the need for the new projectiles to move at a different angle, I added a new field for trajectory angle to the Laser script and modified the movement code from the laser to use the new variable in the calculation. I also exposed the angle to the editor and provided public getter and setter methods so that the angle can be accessed and modified by other scripts.

I then had to populate the trajectory angle on my existing laser prefabs, both player and enemy with their respective movement angles, 0 degrees for the player and 180 degrees for enemy fire. Lastly I wen back into my spray shot prefab and adjusted the trajectory angles of each shot to match the angle that I rotated that shot.

Player script modifications

The next set of tasks to get this new shot working is to modify the Player class to activate and deactivate the shot in much the same way that I do with the triple shot. I add a variable for the prefab to be passed in from the editor and a variable to indicate if the spray shot is active.

In the OnFire method I modify the logic that choose which laser prefab to instantiate to include the new spray shot.

Next, I add the coroutine that deactivates the spray shot after 5 seconds.

And lastly, I add the new powerup activate code to the switch statement that fires when the Player picks up the newly added collectible.

Spawning the Collectible rarely

The last requirement to I need handle is that the powerup needs to spawn rarely. To handle this, I added some code to the SpawnManager in my GetRandomPowerupPrefab method that generates a random number between 1 and 100 (percentages) and then checks that number to see if it is greater than 75, I remove the spray shot from the list of possible powerups and then reroll for a random powerup in the list that is left. This means that 25% of the time the new powerup isn’t even a candidate in the random selection of powerups to spawn. This is a crude algorithm but it meets the requirement since the spray shot is technically spawned more rarely than other powerups. In the Phase 2 requirements I’ll be implementing a more balanced spawn weighting system.

Now that the powerup and new shot are implemented it’s just a matter of playtesting and checking in the code!

Next Time!

In this article we added a new powerup item for the player to pick up that gives them the scatter shot ability. Next time, we’re going to add a scaling bar UI so the player can see how much thruster power they have left! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.

--

--

Christopher West
Christopher West

Written by Christopher West

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

No responses yet