Barrel explosion play sound

Hello:
I am trying to make my barrel play sound when it explode, here is the code I am using.
The sound does not play when the barrel explode.

function OnCollisionEnter(collision : Collision){
    for (var contact : ContactPoint in collision.contacts){
    }
    if(collision.relativeVelocity.magnitude > 2)
    audio.Play();
}

1 Answer

1

Formatting your code using the 101/010 button will make it far more likely that folks will read and answer your question. I formatted it for you this time. There are a number of reasons that this code might be failing. Here is a checklist of things to verify or try:

  • Make sure there is an AudioSource attached to this game object.
  • Make sure the audio source has a clip assigned and the clip will play
  • Make sure that one or the other of the two object colliding have a Rigidbody
  • Put a debug statement inside the OnCollisionEnter() to verify that it is being called.
  • Output the relativeVelocity.magnitude for all collisions to verify that you are getting collisions of magnitude greater than 2.0.