Am I using the wrong Sound Function for this?

Always what should be the simple things.

Trying to get my FPS player that when he walks over a "Meadow Muffin" that he says "Dag Gumit!"

I'm using this in my FPS player script:

function OnCollisionEnter (other : Collision) {
    if (other.gameObject.name == "MeadowMuffin") {
        audio.clip = meadowMuffinCurse;
        audio.Play();
    }
}

I have a varible set up as:

var meadowMuffinCurse : AudioClip;

And I've assigned the apropriate sound in the inspector

My "Meadow Muffin" even has a box collider which is set to "Trigger" but the sound does not play.

1 Answer

1

OnCollisionEnter is fired by the physics system and only work with collisions and therefore with colliders + rigidbody that are not set to trigger.

In your case a trigger is the right choice for that task, but you have to use `OnTriggerEnter` event instead.

function OnTriggerEnter (other : Collider) {
    if (other.gameObject.name == "MeadowMuffin") {
        audio.clip = meadowMuffinCurse;
        audio.Play();
    }
}

Watch out, OnTriggerEnter don't get a Collision as parameter, instead just the intersecting Collider is passed.

OK great, it's working, but it does not play the whole clip. it's such a small object that if my player stands exactly on it it will play the whole clip, but if I pass therough it it just play a very small fraction of the audio clip. How do I fix that?

Maybe you have to increase the Maxdistance value on your audio source. Also check your rolloff curve. Make sure the object where the audioSource is attached to is not deleted. Where is that script attached to? If this is executed on the player, make sure you didn't start another sound on the same Audiosource. That would stop the current one.

That could be it, I have it on my FPS player script but I added a "audio.PlayOneShot(meadowMuffinCurse); yield WaitForSeconds(meadowMuffinCurse.length);" To it and that seems to have fixed it for the most part. There is the odd occasion where the sound cuts short still?