- Home /
How can I make a sound deploy after colliding with a certain object (or terrain)?
Hi everyone. I was wandering if there was a way to make a sound deploy when hitting a specific object/terrain, because I really need it for a game that needs sounds, desperately.
Answer by Eric5h5 · Mar 31, 2010 at 09:37 PM
Check the name or tag of the colliding object in OnCollisionEnter, and play an appropriate sound depending on the results:
var funkySound : AudioClip; var enemyHitSound : AudioClip;
function OnCollisionEnter (other : Collision) { if (other.gameObject.name == "FunkyThing") { audio.clip = funkySound; audio.Play(); } else if (other.gameObject.tag == "Enemy") { audio.clip = enemyHitSound; audio.Play(); } }
Thanks but Could you help me with this? I still don't know how to work it.
@Chrifoldus: You're going to have to be a lot more specific than that.
I $$anonymous$$ean, I Just inserted the code and i select sound but nothing happens.
@Chrifoldus: I'd strongly suggest doing some tutorials and reading the docs so you understand how Unity works. If you used it literally, nothing will happen with the code I posted unless the object the script's attached to collided with a game object named "FunkyThing" or any object tagged "Enemy".
Your answer
Follow this Question
Related Questions
Objects Touching? 1 Answer
Object collision with terrain 0 Answers
Camera Colliding with Terrain 1 Answer
Collision between objects 2 Answers