Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

How can I play a sound every 5 minutes?

Discussion in 'Scripting' started by BlendCraft, Nov 6, 2014.

  1. BlendCraft

    BlendCraft

    Joined:
    Nov 2, 2014
    Posts:
    273
    I need to attach it to the main camera.

    I just want to play some sounds like distant gunshots to add ambiance to the game.
     
  2. doug0102

    doug0102

    Joined:
    Jan 24, 2014
    Posts:
    3
    Code (CSharp):
    1.  
    2. float m_Timer = 300.0f;
    3. void Update ()
    4. {
    5.     m_Timer -= Time.deltaTime;
    6.  
    7.     if(m_Timer <= 0.0f)
    8.     {
    9.         // play sound
    10.         m_Timer = 300.0f;
    11.     }
    12. }
     
  3. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,560
    Use an IEnumerator, where you yield for 5 minutes, then call a play sound method. Doesn't require constant attention during update.

    Edit: It's called a coroutine, I should have mentioned that.
     
  4. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,679
    InvokeRepeating

    Code (JavaScript):
    1. InvokeRepeating("PlayAmbientGunshots", 0, 300.0);
    And the function that will play those gunshots:
    Code (JavaScript):
    1. function PlayAmbientGunshots() {
    2.     // play sound here
    3. }
    You might want to add a brief delay before the first gun shot is played, e.g.
    Code (JavaScript):
    1. InvokeRepeating("PlayAmbientGunshots", 5, 300.0);
    will wait five seconds before playing the first gunshot.
     
    MD_Reptile likes this.
  5. davidsvson

    davidsvson

    Joined:
    Jul 4, 2014
    Posts:
    11
    And if you want it to play at a more random intervall:

    Code (CSharp):
    1. void Start () {
    2.      
    3.         Invoke("PlayAmbientGunshot", 300.0f);
    4.     }
    5.  
    6.     void PlayAmbientGunshot()
    7.     {
    8.         float randomTime = Random.Range(100.0f, 300.0f);
    9.      
    10.         // play sound here
    11.  
    12.         Invoke ("PlayAmbientGunshot", randomTime);
    13.     }
    14.  
     
  6. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,679
    Fine, fine. Let's get super fancy. :)

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. @Range(1.0, 900.0)
    4. public var startDelay : float = 5.0;
    5.  
    6. @Range(1.0, 900.0)
    7. public var shortestInterval : float = 30.0;
    8.  
    9. @Range(1.0, 900.0)
    10. public var longestInterval : float = 300.0;
    11.  
    12. @Range(0.01, 2.0)
    13. public var quietest : float = 0.1;
    14.  
    15. @Range(0.01, 2.0)
    16. public var loudest : float = 1.0;
    17.  
    18. public var soundEffects : AudioClip[];
    19.  
    20. @script RequireComponent(AudioSource)
    21. function Start () {
    22.     Invoke("PlayAmbientAudio", startDelay);
    23. }
    24.  
    25.  
    26. function PlayAmbientAudio()
    27. {
    28.     var randomInterval = Random.Range(shortestInterval, longestInterval);
    29.     var randomLoudness = Random.Range(quietest, loudest);
    30.     var randomSound = Random.value * (soundEffects.length - 1);
    31.     audio.PlayOneShot(soundEffects[randomSound], randomLoudness);
    32.     Invoke ("PlayAmbientAudio", randomInterval );
    33. }
     
    davidsvson likes this.
  7. davidsvson

    davidsvson

    Joined:
    Jul 4, 2014
    Posts:
    11
    Nicely done! :)
     
  8. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,679
    Why thank you sir.
     
  9. gabriead

    gabriead

    Joined:
    Oct 10, 2014
    Posts:
    4
    Hi guys,

    how can I play a sound triggered by a certain game Object appearing in the scene?
     
  10. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,679
    1. Don't thread hijack.
    2. What have you tried to do so far?
    3. There are plenty of examples of this on the forums, I recommend you search there first.