Power Up System in Unity — Part 1
The game is looking amazing so far. It’s time to create some cool features — ‘Power Up system’ is one of the most important core features that will make the game more exciting and interesting.
In this article, I would like to introduce Triple Shot Power Up. The player would collect a Triple Power Up item & able to shoot 3 lasers instead of 1. Ok, let’s get to it!
Creating Triple Shot Prefab
First, let’s create a prefab that we will be using. It’s simple as creating an empty game object (make sure to reset its position to 0,0,0), then add 3 laser prefab under it. You can place these 3 lasers as shown below.
Triple Shot Prefab Behavior
So, what do we need to do in order to be able to switch between single and triple laser shots? We can implement a bool variable that will check ‘is Triple Shot Active or not?’.
In the player script,
- Create a variable of bool ‘isTripleShotActive’ as ‘false’ to start with.
2. Under FireLaser() method, add if statement that will instantiate a tripleshot prefab IF ‘isTripleShotActive’ is true. Else, instantiate singleshot prefab.
Create TripleShot Power-Up that can be collected
Now that we have a tripleshot laser prefab created, it’s time to create another prefab that we can collect and enable tripleshot ability.
Again, this power-up prefab is essentially a sprite 2d element. In Inspector window, let’s add Rigidbody 2D as well as Circle Collider 2D (or choice of yours).
Don’t forget to the following as well:
- In Circle Collider2D — check ‘Is Trigger’ under Collider! (We will need this to use OnTriggerEnter() Method)
- In Rigidbody2D — Gravity Scale to 0, so that we don’t get affected by gravity.
Once you’ve done that, let’s create a new C# script called, ‘PowerUp’.
This powerup prefab will come down from the top of the screen (similar to the enemies). In this script, we will do the following:
- move it down at speed of 3, then destroy it when it is at the bottom of the screen.
- When it collided with the player (by looking for a tag “Player”), we will enable the bool variable ‘isTripleShotActive’ (that we created above) to true. Then, destroy it.
It’s working properly. Awesome! In the next article, I will go over how we can spawn every 5–10 secs randomly + simple animation for PowerUp Prefab!
Thank you so much for reading!