Adding a Powerup

Christian Carter
3 min readMay 27, 2021

Powerups are a great way to add some more fun to your game. In this example you are going to add a triple shot powerup to the game. Start by dragging the powerup sprite into the Hierarchy to create the object. Adjust the size as needed and set the layer to foreground. Add in either a box collider 2D or a circle collider 2D. Whichever you feel fits best. Check the isTrigger box. Add a rigidbody 2D and set the gravity scale to 0. Now create and add a powerup script to the object. By now this should all be fairly standard procedure for creating a new object. You can now move it to your prefab folder.

Next, you need to setup the script. Getting the powerup to move down the screen is just like having other objects like the enemy and the laser move. You will need a speed variable and will then use it in the tranform.Translate function. Then choose a point when it has left the bottom of the screen to destroy it. See the code below.

[SerializeField] private float _speed = 3.0f;// Update is called once per frame
void Update()
{
transform.Translate(Vector3.down * _speed * Time.deltaTime)
if (transform.position.y < -6.0f)
{
Destroy(this.gameObject);
}
}

For the on trigger enter event you will want to check the tag and make sure it is the player. If it is you need to get the player component. Here you will call a function from the player to activate the triple shot. This will be covered further down. After the triple shot is activated you will need to destroy the object. See the code below.

private void OnTriggerEnter2D(Collider2D other)
{
if (other.transform.tag == "Player")
{
Player player = other.transform.GetComponent<Player>();
if (player != null)
{
player.TripleShotActive();
}
}
Destroy(this.gameObject);
}

Now it’s time to create the triple shot. To do this create an empty object and add three lasers to it. Space the lasers as needed to fit your spaceship. Move the triple shot to the prefab folder.

Now go to the player script. First add a couple of variables. One to track if triple shot is active and one to reference the triple shot prefab. Make sure you connect the prefab in the editor.

[SerializeField] private GameObject _tripleShot;
[SerializeField] private bool _isTripleShotActive = false;

Next, update the FireLaser function to check if triple shot is active. If it is active instantiate the triple shot instead of the regular laser. See the code below.

void FireLaser()
{
_canFire = Time.time + _fireRate;
if (_isTripleShotActive)
{
Instantiate(_tripleShot, transform.position, Quaternion.identity);
} else
{
Instantiate(_laserPrefab, transform.position + new Vector3(0, 1.05f, 0), Quaternion.identity);
}
}

Now update the player code to add the function you called in the powerup script. In it first set the triple shot to active. Then start a coroutine that will used to deactivate the triple shot. Now create the IEnumrator function that will be used to power down the triple shot. Set it to wait for 5 seconds then set the triple shot to not active. See the code below.

public void TripleShotActive()
{
_isTripleShotActive = true;
StartCoroutine(TripleShotPowerDownRoutine());
}
IEnumerator TripleShotPowerDownRoutine()
{
yield return new WaitForSeconds(5.0f);
_isTripleShotActive = false;
}

Now the triple shot powerup is working. There is only one problem, it leaves lots of empty triple shot objects in the hierarchy window. You can fix this in the laser script by checking if the laser has a parent and if it does destroy the parent.

if (transform.parent != null)
{
Destroy(transform.parent.gameObject);
}

Now you are powered up!

Good Luck Adventurer!

-Chris

--

--