GameDev : PowerUp!!
Now we need to create a Power Up system.
So, when we collect Power Up, we can use Triple Shot!
In my project I have this
Firts, drag and drop it in the Scene
Is too big, so we scale it at 0.5, and rename it to Triple Shot Powerup.
We need to collect it, this means we need to add a Box Collider 2D or a Circle Collider 2D. It’s not important what Collider we use, the impoertant thing for funcionality is code, and OnTriggerEnter2D method works absolutely fine with every type of Collider 2D.
And remember to check toggle “Trigger”.
We also need a Rigidbody2D, and set the Gravity to 0.
To create a behavior, we need a new C # script, “Powerup”
Attach it, and make Powerup a prefab, so we can instantiate and reuse it.
Now, Pseudo Code and we try to understand what we want to do and how do it
Ok, first, need speed variable.
Then, we try to write some code
Here we have :
two variables, one for speed and one for check if Powerup is go out of the screen.
And the Translate method to move Powerup down, with Vector3.down multiplied by speed and Time.deltaTime.
Then we check : if our position y is less than 6.0f, destroy this gameObject.
Let’s try and see what happen
The first part works.
The Power Up goes down and when it is off the screen it self-destructs.
Now, second part is about OnTriggerEnter2D
What we do here is a simple check:
If the object we collide with is the Player, Destroy.
Try again
And everything works fine!
In the next article we create a better system for our Powerup!