Audio triggering

I have an audio clip (a monster growling) attached to an enemy, and I’d like to be able to play the clip back whenever the player is within a certain distance. I’d like it to repeat but at random intervals from 3-8 seconds. If the player moves out of range I want the sound to stop altogether.

I have the code for the distance check working fine, and I’m using it to set a boolean

var player : Transform;
function FixedUpdate () {
var dist = Vector3.Distance(player.position, transform.position);
if (dist < 15)
inRange = true;
else
inRange = false;
}

Here’s the Audio Script

var monsterScream : AudioClip;

function FixedUpdate () {
if (justProximity.inRange)
audio.PlayOneShot(monsterScream);
}

I can’t figure out how to get the audio clip to be called only when I want it instead of every frame like FixedUpdate. I’m trying to avoid using OnCollisionEnter because I’m using the RigidBody of the enemy to detect when it has been hit my a missle.
Should I be using OntriggerEnter/Exit?

Any help is appreciated[/code]

//Update - or fixed
function Update () {
    if (!audio.isPlaying) {
        var randomSeconds = Random.Range(0, 10);
        yield WaitForSeconds(randomSeconds);
        audio.Play();
    }
}

OK… Try that.

Cheers,

This works similar to what I have, but the next sound begins immediately after the previous one finishes.

What I’m looking for is way to put a variable amount of silence between each roar, but the player should only hear the sound when in range of the enemy.

Kevin

Aren’t you trying to make Update a Coroutine?

In this case, I think Unity ignores the "yield WaitForSeconds(randomSeconds); " line.

Put this

 var randomSeconds = Random.Range(0, 10); 
        yield WaitForSeconds(randomSeconds); 
        audio.Play();

in a courtine and call it in Update.

Edit:

Dind’t check this code, should work though.

//This variable is used to make sure that Unity is only Executing one Random interval roar at a time.
bool inRoar = false;

function RandIntervalRoar()
{
    inRoar = true;
    var randomSeconds = Random.Range(0, 10); 
    yield WaitForSeconds(randomSeconds); 
    audio.Play();
    inRoar = false;
}

function Update()
{
    if( !audio.IsPlaying()  !inRoar )
    {
        StartCoroutine( "RandIntervalRoar" );
    }
}

Hey I dont want to take this too off topic, but whats the diference between this:

function Update() 
{ 
    if( !audio.IsPlaying()  !inRoar ) 
    { 
        StartCoroutine( "RandIntervalRoar" ); 
    } 
}

And this

function Update() 
{ 
    if( !audio.IsPlaying()  !inRoar ) 
    { 
       RandIntervalRoar(); 
    } 
}

Is there any difference?

Thanks IA
AC

I’m used to writing Scripts in C# in which you have to write “StartCoroutine( “MethodName” );”. I wasn’t sure if you had to write it like that in JavaScript as well.

The only difference I know of is that if you don’t use StartCoroutine then you won’t be able to call StopCoroutine on it, but if you don’t need to, then there’s essentially no difference in terms of the results. Not sure if that changed in 2.0 though.

–Eric

Thanks Der Dude - that did the trick for me.

Kevin

No Problem.

After I looked at the Code again just now, I noticed a performance tweak.

function RandIntervalRoar() 
{ 
    var randomSeconds = Random.Range(0, 10); 
    yield WaitForSeconds(randomSeconds); 
    audio.Play(); 
    enabled = false;
} 

function Update() 
{ 
    if( !audio.IsPlaying() ) 
    { 
        StartCoroutine( "RandIntervalRoar" ); 
        enabled = false;
    } 
}

This way the script won’t calculate the distance between the monster and the player, if it is already in RandIntervalRoar. Of course the distance calculation isn’t implemented in my outtake, but I think you know what I mean.