Boss Health and Music

No Boss Music, No Boss fight

Tristan Engel
2 min readAug 4, 2021

Today we will be doing two things: giving the boss a health system and starting boss fight background music at the right time.

Boss Music

We put an Audio Source component on our Boss with a boss stage music. When the boss is defeated the music will stop as well.
In the inspector note that ‘Play On Awake’ is turned off.

In our script we will use AudioSource.PlayDelayed to start playing the music.

Boss Health Script

A simple script that starts playing the boss music with delay in Start().
Boss health can only be damaged by objects with tag Laser and the player ramming the boss will only damage the player.
Last but not least we need an explosion on the death of our boss.

[SerializeField] GameObject _explosionPrefab;

private int _bossHealth = 10;

private AudioSource _audioSource;
private float _delay = 7f;

void Start()
{
_audioSource = GetComponent<AudioSource>();
_audioSource.PlayDelayed(_delay);
}

private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
other.GetComponent<Player>().Damage();
}
else if (other.tag == "Laser")
{
Destroy(other.gameObject);
_bossHealth--;
if (_bossHealth <= 0)
{
BossDeath();
}
}
}

private void BossDeath()
{
//VFX Explosion
Instantiate(_explosionPrefab, transform.position, Quaternion.identity);
Destroy(gameObject);
}

--

--

Tristan Engel
Tristan Engel

Written by Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

No responses yet