Boss Health and Music

No Boss Music, No Boss fight

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);
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tristan Engel

Tristan Engel

18 Followers

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