My journey becoming a Unity game developer: Super Shot & its Powerup
Objective: Create a Super Shot Powerup, set up its behavior, and animate it. Also, create the SuperShot moving in a Cosine wave pattern and its behavior.
Start by dragging the powerup sprite into the Hierarchy, and name the powerup SuperShot_Powerup. Add a Box Collider2D, Rigidbody2D, and the Powerup script. Make sure the box collider IsTrigger is turned on, and the rigidbody Gravity Scale is set to zero. Also, we change the color of this powerup sprite through the Sprite Renderer.
Create a new powerup animation name SuperShot_Powerup_anim. Add all of the powerup sprite animations onto the dopesheet and play to see if it’s working.
To make this powerup standout among the others, let’s flip this upside down by going over to the Sprite Renderer. In the Flip setting, turn on the X and Y axis to flip the sprite texture along the checked axis. This does not flip the Transform position of the GameObject.
.
Plus, let’s add a Particle System as a child to the SuperShot game object to help it stand out for the player to see. There are a lot of settings to play around with, but I just kept this simple for now. I played around with some of the base settings. Also, I played around in the Emission, Shape, and Renderer Modules.
Over in the Player script, we will add new variables representing the SuperShot game object, the audio clip to play each time the SuperShot is fired, if the SuperShot laser is being used or the regular laser, and if the SuperShot is active to use.
Create a method that will activate if the SuperShot powerup has been collected. If it has we will activate SuperShot and the SuperShot Laser. We want to stop the regular laser sound from playing, change to the SuperShot audio clip, and play the new clip. Last, start the cool down coroutine to turn off the SuperShot and its laser. In the coroutine, we will wait 5 seconds before turning off the SuperShot, the SuperShot Laser, and stop the SuperShot audio from playing.
In the PowerUp script, I added a new audio clip to make sure the player knows this powerup is the SuperShot. Inside the OnTriggerEnter2D under the first powerup audio clip, we will add another if statement to check if the powerupID number equals 4. If it does, we will play the new audio clip at the particular spot in world space the powerup was collected.
Also, in the Switch condition checking for powerupID’s, add CASE 4 which will represent if the SuperShotBoostActive() method inside the Player script is active.
In the Spawn Manager game object, change the Powerups array value from 4 to 5. Then in Element 4 add the SuperShot_Powerup prefab to this location.
Go to the SpawnManager script, inside the PowerupSpawnRoutine() method change the Random Range to 5 in the randomPowerup variable. This will include the SuperShot powerup to be instantiated.
Now we will create the SuperShot itself. First, create an empty game object and name it SuperShot. Set its Transform positions to zero which will allow it to be positioned to the player’s spaceship. Right-click on SuperShot and create a new C# script name SuperShot that will be attached to this game object. Last, make the SuperShot into a Prefab.
Second, inside the script we want the SuperShot to be attached to the player’s position at all times. Create a Player variable to be the handle. In the Start() function, use the variable to get the player’s component. Then in the Update() function, assign the SuperShot’s transform position to the player’s transform position.
Third, let’s create the SuperShot Laser by dragging the Laser sprite into the Hierarchy and rename it to SuperShot_Laser.
Change the Scale of it to differentiate itself from the other laser, change the Sorting Layer to Foreground in the Sprite Renderer. Add a Box Collider2D, adjust the collider around the laser and turn on Is Trigger. Add a Rigidbody2D and make Gravity Scale equal zero. Finally, right-click on the SuperShot_Laser and create a new C# script name SuperLaser.
Fourth, in the SuperLaser script add variables that will control how the lasers will move using Cosine wave patterns. In this case it will be Frequency for how fast the lasers move. Amplitude for how wide the lasers will move on the X axis. Speed for how fast the lasers will move up the screen. Also, create a variable to get the player’s component again. Last, create a variable that will assign the laser’s starting position relative to the SuperShot’s game object position.
In the Start() function, assign the player variable to find the Player’s component. Then assign the starting position to the Parent’s X-axis position when the SuperShot is active. Use NULL check to check if the player’s component is available.
We will create a new method call MoveUp() to physically move the laser up the screen as well as from side to side. Create 3 float variables that will assign each position of the laser’s axis. Since we’re moving along the X-axis, let’s use the Cosine method which will take in time as its parameter first. When the SuperShot Laser is active, we need to create a new transform position that will calculate the Cosine pattern and add that to the starting position. Keep the Y and Z axis the same. If you wanted to move along the Y-axis, use the Sine() wave method just like you use the Cos() method. To move the lasers upward, use the transform Translate() function to go up in real-time with the amount of speed you choose. Now, check IF the lasers have move beyond a certain amount of units offscreen to destroy them. It’s crucial to destroy the Parent object instead of the lasers themselves because if you don’t destroy the Parent they will still remain alive in the game which can present problems further along in the game. When you destroy the Parent object, you’re also destroying all of their children in the process. Finally, inside the Update() function, use the MoveUp() method to control the laser’s behavior.
Use the OnTriggerEnter2D() function to handle what to do when the laser collides. The one thing we want to check IF it collided with is the Enemy in particular. To check, get the Enemy’s tag and if we collide with them, we need to get the Enemy component first.
Then if the Player component isn’t NULL, add 10 points for each enemy hit. After that we need to get the Enemy’s Animator component and set its Trigger to OnEnemyDeath animation. We want to stop the enemy from moving down the screen after it’s destroyed by turning off the Enemy’s Speed.
Also, we need to play the Enemy’s death audio clip. Finally, we must destroy the Enemy script component so the Enemy is disabled. Destroy its Collider2D because the Enemy is technically active onscreen and if you collide where the Enemy died, you will die too. Destroy the SuperShot Laser itself and Destroy the Enemy in the approximate time it will take for the Enemy death animation to play completely.
Finally, in the Player script, go to the FireLaser() method, we need to check IF the SuperShot is active. If so, Instantiate the SuperShot Prefab and take 2 lasers away every time its called.