Let’s Spruce Up That Triple-Shot!
Objective: modify the existing “triple-shot” featured in my current game project to a more lethal dispersion.
As I continue my journey through the GameDevHQ (GDHQ) 2D Game syllabus, I’ve often find myself going down these rabbit holes. No real reason other than I simply imagine something different than what’s presented in front of me as I learn the basic concepts of game development. Let’s take for example the behaviour of the player’s laser.
We began by setting up a single laser emerging from the center nose of the spaceship. Well, I found that was simply too little firepower, so I set my own project with a “double-shot” as its default weapon. Nothing too fancy, just an instantiation of a “dual laser” prefab. Then came the section in the syllabus on game power ups, and the “triple-shot” was introduced.
Again, fairly simple. Once the player captures the “triple-shot” power up, it flips the _isPlayerTripleShotActive bool from false to true. Now the original concept from GDHQ had a single shot fired from the nose, and two other shots from the wingtips. As a result, it made for a more impressive increase in firepower since the path of destruction took on the width of the spaceship.
If you compare my own modified interpretations, other than bumping my laser shots from 2 to 3, the lethal kill zone in front of my ship remains the same. You won’t scare away invading alien hordes with this setup! And that is how I came to my first rabbit hole! Bye-bye syllabus deadlines. I was now on a mission to work out a way to make my triple-shot more lethal and visually appealing. The inspiration came from old video games of my youth where you see the projectiles fired from the player fan out at an angle.
It took me a while to find a few examples from other developers (thank-you Google Gods!), and while I openly admit that I’m still struggling to comprehend how the magic of Quaternions and Euler angles happen, I managed to grasp enough information to adapt a workable solution to my self-induced challenge.
I started out by declaring two new variables. A private int called NumberOfProjectiles to store the amount of shots to be fired simulatneously, and a private float called SpreadAngle, set by default to 30, with a slider bar to adjust the spread angle anywhere from 0 to 360. You make a slider bar by writing [Range(0, 360)]. Setting these up as [SerializeField] allows some fine tuning in the Inspector.
I then needed to figure out how to spread the shots evenly within the desired spread angle. To do this, I divided the _spreadAngle by the _numberOfProjectiles, and stored it in a float called angleStep. To figure out the offset from the center point of aim (nose of the spaceship going straight up the game scene), I took half of the angleStep, and deducted from it half of the _spreadAngle.
By using the setting above (number of projectiles = 3, spread angle = 30), I get a angleStep of 10, and a centeringOffset equal to ((30/2) — (10/2)) or (15–5) for a result of 10.
I then ran a loop to instantiate a single laser prefab (_playerLaserPrefab) for each of the _numberOfProjectiles. So we get a loop that looks like this:
(int i = 0; i < 3; i++)
Every time we run through the loop, we store a new float variable called currentBulletAngle for each projectile. We then use that data to set the new Vector3 for the Quaternion.Euler, which in turn is stored in the variable “rotation”.
First: currentBulletAngle (10 * 0 )= 0, Euler Vector3 (0, 0, -10)
Second: currentBulletAngle (10 * 1)= 10, Euler Vector3 (0, 0, 0)
Third: currentBulletAngle (10 * 2) = 20, Euler Vector3 (0, 0, 10)
Once we have everything we need figured out, we only need to instantiate each individual _playerLaserPrefab at the position of the player’s spaceship, adding a slight offset to the y-axis to have the triple-shot laser generate from the nose of the ship. The entire code snippet looks like this:
And here we see the final result. Pretty sweet, eh?
Again, I’m still trying to wrap my brain around exactly “how” Quaternions and Euler angles work, and the behavior above came after a lot of trial and error. Regardless, I’m hoping this will serve as a stepping stone for some of you out there, and assist in implementing similar features in your own interpretation of the traditional Space Shooter style game project. Thanks for reading!