- Home /
only one onCollisionEnter event is detected
Hi there. I just started out learning unity so please bear with me:
I have a two cubes in my scene. One cube is a floor and the other is a small cube object above the floor.
The small cube object is a rigid body, uses gravity and the material is bouncy.
I want to play a sound when the cube falls and collides with the floor. So i coded this:
using UnityEngine;
using System.Collections;
public class boxCollision : MonoBehaviour {
public AudioClip collisionSound;
void OnCollisionEnter(Collision theCollision){
if(theCollision.gameObject.name == "Floor"){
AudioSource.PlayClipAtPoint(collisionSound, transform.position);
}
}
}
now when i attach a sound file, and play the game, the cube drops and the sound is played, but when it bounces back up and hit the floor again, the sound is not play.
Can you explain me why it is not playing the sound again?
Is it playing when the collision happens or right as soon as you hit play? pick your cube up and make sure its playing exactly when the collision happens. Im having trouble with your code.
Answer by darthbator · Jun 03, 2013 at 11:42 PM
A few things. One is to make sure it's bouncing clear out of the trigger or collider. Try throwing out a debug message OnCollisionExit to make sure the cube is actually leaving the first collision and not just continuing to triggger OnColliderStay.
The other would be to mark your sound stereo so you're sure to hear from any location in the scene.
Your answer
Follow this Question
Related Questions
Play sound on collision 1 Answer
How to get sound to play on barrel Explosion 1 Answer
Dice roll collision sound 1 Answer
Making a sound using collisions 1 Answer