Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Randomly play sounds on collision detection point

Discussion in 'Scripting' started by Dari, Jun 17, 2013.

  1. Dari

    Dari

    Joined:
    Mar 25, 2013
    Posts:
    130
    I have this script and it plays only one sound on collision point, I want to play randomly sounds which I can set:

    Code (csharp):
    1. var ricochetSounds : AudioClip;
    2. var playerDead : AudioClip;
    3.  
    4. function OnTriggerEnter(collision : Collider) {
    5. AudioSource.PlayClipAtPoint(ricochetSounds, transform.position);
    6. Destroy(gameObject);
    7.  
    8. if(gameObject.name=="Human(Clone)")
    9. AudioSource.PlayClipAtPoint(playerDead,transform.position);
    10. }
    11.  
    Can anyone help me create that it plays random sounds at collision point? Thanks!
     
  2. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    have your "random sounds" placed in a simple AudioClip array named in this example AudioClips, and in your if statement, use this:
    Code (csharp):
    1.  
    2. if(gameObject.name=="Human(Clone)"){
    3. AudioSource.PlayClipAtPoint(AudioClips[Random.Range(0, AudioClips.Length)], transform.position);
    4. }
    5.  
     
  3. Dari

    Dari

    Joined:
    Mar 25, 2013
    Posts:
    130
    Thanks alot, it works great!!! :D
     
  4. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    no problem mate!