using System.Collections; using System.Collections.Generic; using UnityEngine; using Assets.Scripts.CharacterScripts; public class BossMusicController : MonoBehaviour{ public AudioSource music; // Reference to the general level music public AudioSource bossMusic; // Reference to the boss music public PlayerController player; // Reference to the player script public float fadeDuration = 2f; // Duration of fade-in and fade-out bool hasPlayedBossMusic = false; void Start(){ // Initialize the player reference player = GameObject.FindGameObjectWithTag("Player").GetComponent(); } void OnTriggerEnter2D(Collider2D other){ // Check if the player entered the trigger area and boss music hasn't played yet if (other.CompareTag("Player") && !hasPlayedBossMusic){ hasPlayedBossMusic = true; // Start fading out the general background level music and fade in the boss music StartCoroutine(FadeOutGeneralMusicAndFadeInBossMusic()); } } IEnumerator FadeOutGeneralMusicAndFadeInBossMusic(){ float startVolume = music.volume; float fadeStep = startVolume / fadeDuration; // Fade out the general level music while (music.volume > 0){ music.volume -= fadeStep * Time.deltaTime; yield return null; } // Ensure volume is set to 0 music.volume = 0; // Stop the general level music music.Stop(); // Start playing the boss music bossMusic.Play(); // Fade in the boss music while (bossMusic.volume < 1){ bossMusic.volume += fadeStep * Time.deltaTime; yield return null; } // Ensure volume is set to 1 bossMusic.volume = 1; } }