Probability-based/multiple sounds on one object Unity

Updated on April 30, 2017 in [A] Audio
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on April 30, 2017

How can I add Probability-based sound or random sound on an object (trigger) in unity?

The script that I am currently using; 
JS
————————
#pragma#333333;”> <span style=”color: #333333;”>strict<br />
var sound : AudioClip 

function Start () 

{
    var audio : AudioSource = GetComponent.<AudioSource>() ;
}

function OnTriggerEnter (Col : Collider
{
    if(Col.CompareTag (Player))
    {
        GetComponent.<AudioSource>().Play(); 
    }
}
————————

Thanks for the help

  • Liked by
Reply
0 on April 30, 2017
  • Liked by
Reply
Cancel
0 on April 30, 2017

I figured something out in case anyone wants to use a random probability for an interesting/unique sound experience every time their game is played. Here is the script I made up, it’s not going to be the neatest as I’m a complete novice coder!

#pragma<span style=”color: #333333; text-decoration: underline;”> strict

var sound : AudioClip ; 
var sound1 : AudioClip ;
var sound2 : AudioClip ;

function Start () 
{
    var audio : AudioSource = GetComponent.<AudioSource>() ;

// Creating variables that are audio based 
// it will look for an audio source, then get it and play it 

    if (Random.value > 0.4) {
        audio.clip = sound;}
    else if (Random.value > 0.3) {
        audio.clip = sound1;}
    else {
        audio.clip = sound2;}
}

//Random.value uses random probability from 0  1
//to select an audio clip and play it making for a different sound experience every time the game is played

function OnTriggerEnter (Col : Collider

{
    if (Col.CompareTag (Player));

    {
        GetComponent.<AudioSource>().Play(); 
    }

  • Liked by
Reply
Cancel