How to create a Triple Shot
Including a basic power up system
We already have a normal laser. In this article we will create a Triple shot variant. To activate the triple shot it we will create a power up too.
Triple Laser Prefab
1. Duplicate Laser twice — unpack prefab completely if needed
2. Align the left and right laser with the Player
3. Create empty container — LaserTriple
4. Put all 3 lasers in container
5. Prefab — LaserTriple
6. Adjust Laser script to destroy parent too if it exists when going off screen
The code to check for a parent and if it exists destroy it. Only used when laser is off screen.
//check if object has parent
if (transform.parent != null)
{
//then destroy parent
Destroy(transform.parent.gameObject);
}
Script Player
1. Create Reference to Triple Laser prefab
2. Drag prefab into Inspector box
3. Create bool to check activation status of powerup
4. Create public function to activate powerup
public void PowerUpPickUp(string PowerUpType)
{
if (PowerUpType == "TripleShot")
{
_isTripleLaserActive = true;
//start coroutine
StartCoroutine(TripleShotCooldown());
}
}
5. Create coroutine to let the powerup end after X seconds
IEnumerator TripleShotCooldown()
{
yield return new WaitForSeconds(5f);
_isTripleLaserActive = false;
}
Powerup creation
Yesterday we animated a powerup game object, which we are going to use today.
Script Powerup
1. Create Powerup script
2. Add script to powerup gameobject
3. Make object move down
4. Destroy object when off screen
5. Collision detection if tag == player
6. Then access player script and activate triple
private void OnTriggerEnter2D(Collider2D other)
{
//if collision tag is Player
if (other.tag == "Player")
{
//get player script //activate power up triple shot
Player player = other.transform.GetComponent<Player>();
if (player != null)
{
player.PowerUpPickUp("TripleShot");
}
Destroy(this.gameObject);
}
}
Spawn Powerup
Finally add create a powerup spawn coroutine just like the one we created for enemies in the article below.