Categories
Uncategorized

Audio with Unity

To incorporate the sounds into this game a custom script was written which handles the audio clips. There is a Master clip and 2 children.

public class zonemusic1 : MonoBehaviour
{ 
public audiomaster audiomaster;
public AudioClip clip1;


void OnTriggerEnter(Collider other)
{
        if (other.CompareTag("Player"))
        {
            audiomaster.play(clip1);  //from audiomaster

        }

    }

void OnTriggerExit(Collider other)
{
        if (other.CompareTag("Player"))
        {
            audiomaster.stop();
        }
    }
}

zone music handles the audio source collision logic for the audio master. There are 3 empty game objects, one parent and two children. The variables for the audio source and audio master are assigned in the inspector window as follows.

The sound master uses the following code to handle playing the first song throughout the login/character selection/and first zone. This seamless transition is then altered when collisions are detected n the zonemusic class. There is logic for a slider that is then assigned for the soundmaster to be handled in the main menu.

public class audiomaster : MonoBehaviour
{
    public Slider volumeSlider;
    public AudioSource audiosource;
    public AudioClip defaultClip;  //the default clip that will play at start up
    void Start()
    {
        audiosource.clip = defaultClip;
        audiosource.Play();
        
    }
    

    public void play(AudioClip clip1){
        if (clip1.name == audiosource.clip.name && audiosource.isPlaying) { return; } //if were already playign the default clip
audiosource.clip = clip1;
        audiosource.Play();
}

public void stop(){
        if (defaultClip == audiosource.clip) {  return; }
        audiosource.clip = defaultClip;//when exiting trigger can revert to default audioclip
        audiosource.Play();
    }

    void OnEnable()
    {
        //Register Slider Events
        volumeSlider.onValueChanged.AddListener(delegate { changeVolume(volumeSlider.value); });
    }

    //Called when Slider is moved
    void changeVolume(float sliderValue)
    {
        audiosource.volume = sliderValue;
    }

    void OnDisable()
    {
        //Un-Register Slider Events
        volumeSlider.onValueChanged.RemoveAllListeners();
    }
}

Variables for the slider and Default clip are assigned in the inspector as follows.

2 replies on “Audio with Unity”

Thanks for your own labor on this web site. My mother take interest in participating in research and it is simple to grasp why. My spouse and i notice all regarding the powerful way you produce valuable tips via your website and in addition recommend response from the others on this subject then our own princess is undoubtedly studying a whole lot. Take pleasure in the rest of the year. You have been performing a terrific job.

Thanks for your entire labor on this web site. My mother take interest in participating in research and it is simple to grasp why. My spouse and i notice all regarding the powerful tactic you produce useful thoughts via your website and in addition recommend response from the others on this subject then our own princess is undoubtedly studying a whole lot. Take pleasure in the rest of the year. You have been performing a terrific job.

Leave a Reply

Your email address will not be published. Required fields are marked *