Add audio.

Updated on October 3, 2017 in [A] Audio
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on September 30, 2017

Two questions, and please explain well. First, how can I play an audio clip five seconds after the game starts?

Second, how can I play an audio clip when the player passes certain places in the level.

– ALSO, THIS IS A 2D PLATFORMER.

  • Liked by
Reply
0 on October 3, 2017
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
    //your source where the audio comes from
    public AudioSource audioSource;
    //your audio file
    public AudioClip audioClip;
    private void Start()
    {
        //starts the function in 5 seconds
        Invoke("PlayAudioClip", 5f);
    }
    private void PlayAudioClip()
    {
        //add the audio clip to the audiosource
        audioSource.clip = audioClip;
        //plays the audiosource
        audioSource.Play();
    }
    //put the following into a script which is attached to the player gameObject
    private void OnTriggerEnter(Collider other)
    {
        //create a collider in unity and tick the box for "IsTrigger";
        //also give the gameObject a tag named e.g.: "Level Event"
        if(other.tag == "Level Event")
        {
            PlayAudioClip();
        }
    }
}
  • Liked by
Reply
Cancel