x


Audio collisions

I have an gameobject that has a box collider and an audio source. It also has a Javascript that has a single function:

function OnCollisionEnter(collision : Collision) {
    audio.Play();

}

The audio is not played when a collision happens. What is the problem?

more ▼

asked Dec 09 '10 at 06:50 PM

Mixel gravatar image

Mixel
31 1 1 4

(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

The code looks fine. Make sure that

  • Your game object has a sound attached.
  • You have a collider to collide against.
  • Either one of the object should have a rigidbody.
  • Your camera has a listener.
  • Your camera isn't very far away from the sound.
  • Your speakers are on.

See the collision action matrix at the bottom of this page to see what messages are sent in different circumstances. You can further debug this issue using a print("Collision Detected"); just before audio.Play();

  • Then if you don't get the debug message, the collision isn't happening.
  • If you get the message but no sound, something's still wrong with the sound.
more ▼

answered Dec 09 '10 at 09:33 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

you don't need a rigidbody to make that function work. it says collider/rigidbody so a normal preset collider or a rigidbody.

Dec 09 '10 at 10:10 PM Jesus_Freak

You're not going to get OnCollisionEnter with two colliders without either one of the two objects having a rigidbody. Try it. I did. You must have misunderstood.

Dec 09 '10 at 10:48 PM Statement ♦♦
(comments are locked)
10|3000 characters needed characters left

all you have to do is

if(collision.gameObject.FindWithTag("Audio"))// you can rename the tag here to whatever you tag the thing you want to hit to play the attached sound.
{
 audio.Play();
}

make that adjustment, and replace it into your OnCollisionEnter function. really simple. hope this helps!

more ▼

answered Dec 09 '10 at 10:06 PM

Jesus_Freak gravatar image

Jesus_Freak
1.2k 38 44 59

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x227
x170

asked: Dec 09 '10 at 06:50 PM

Seen: 774 times

Last Updated: Dec 09 '10 at 06:50 PM