Phase 2: Enemies Destroy Collectables

Simon Leen
2 min readJun 29, 2021

--

Objective: Allow enemy lasers to destroy collectables

To allow this we open the powerup script and within the OnTriggerEnter we check if the other tag is EnemyLaser then if the collectable is visable on the screen. To add a bit more to this and rather than just destroying the collectable we create a coroutine that gets the SpriteRenderer on the collectable and through a set of yields set the sprites alpha or visualablitly to decrease while playing a sound specific to a collectable being destroyed.

To get this working we need to create a SpriteRenderer and assigning it in the start method. We also add a new serialized audio clip for when destroyed and assign it in the inspector.

Inside the if other is enemy laser and collectable transform position on the y is less than 5, start the coroutine, then play the audio clip using the audio source play clip at point function and destroy the enemies laser.

If other tag is EnemyLaser

Inside the coroutine we create a color with full alpha(opacity) then wait and repeat making the alpha smaller each time until we destroy the collectable.

Coroutine for collectable

In the next post, we will look at setting up Enemy Waves

--

--

No responses yet