- Home /
collision sounds
i have a chair that is animated and ends up hitting a wall, i have a box collider when the chair hits but the audio doesnt want to play, i have tried doing it a few ways but it wont work. any ideas? thanks
#pragma strict
var myClip : AudioClip;
function OnTriggerEnter (other : Collider) {
if(other.tag == "chair"){
audio.PlayOneShot(myClip);
}
}
Do you have an audiosource attached to the object? Do you have an audiolistener enabled in the camera? Are you sure the code is executed (add Debug.Log inside the if statement)?
if i change it change it to look for player as tag then it works
Answer by greencvbn · Dec 16, 2013 at 02:46 PM
if the chair is tagged in the inspector as a chair i do not see a problem but if the chair is the name of the object then change tag to name .disclaimer this could be wrong.
Answer by Tomer-Barkan · Dec 16, 2013 at 02:52 PM
Since it's not logging inside the if, either the collision is not triggering (try debugging outside the if to see if it logs) - in which case you have to make sure that one of the object has a rigidbody, both objects have colliders, the colliders are marked as trigger, and that they're 3d colliders and not 2d colliders.
If it is logging, then the problem is with the tag checking, the tag of the object you're colliding with might not be "chair".
ive set it to a rigid body and now it works but only if it falls on the collider, if i try and freeze the y axis it does not work, any ideas? thank you
I have no idea what you are trying to do so you'll have to tell me more than just "falls on the collider". Describe your scene and your problem.
ok, ive got a chair that is animated and when it finishes playing it hits in to the wall, and on that wall where the animation finishes is a box collider. i want a sound to play when the chair hits that box collider, if i make the box collider a rigidbody it falls through the map, but if i move the box collider above the chair and it falls on to the chair it makes the sound i want, but if i put it back in the position i want it and uncheck gravity or freeze the axis, the sound will not play. you with me now?
$$anonymous$$aybe it's better to add the rigidbody to the chair, and make it kinematic. That way it won't be affected by forces, but it will trigger collisions.
Answer by O8PC · Nov 27, 2014 at 05:48 AM
is your wall a trigger? if it's not, than you might want to try this:
#pragma strict
var myClip : AudioClip;
function OnCollisionEnter (other : Collider) {
if(other.tag == "chair"){
audio.PlayOneShot(myClip);
}
}
Your answer
Follow this Question
Related Questions
OnCollisionEnter Issues 2 Answers
Vehicle help 1 Answer
How should I make this Audio play? 1 Answer
Stream music in Unity mobile 0 Answers
Prevent items/player from going through walls/floor 1 Answer