- Home /
Animation + sound on collision
I tried many scripts to make this work but i can't find anything that i want.
I am making a scary game and what i want is when the fps collides through a invisible and untoucheble cube, a animation has to start and it has to make a creepy sound (only 1 time).
The animation is called: Ghost 1
The sound effect is called: Woman_Scream
I hope someone can help me, thanks
Yep that's it - make the cube collider a trigger. Use OnTriggerEnter to trigger your animation and at the same time Destroy(cubeGameObject) so that it doesn't happy again.
Answer by Jakob Steinn · Jul 05, 2012 at 08:31 PM
Actually it is fairly easy, and just to elaborate on what whydoidoit said:
Fist make a box of any size
Tick the "Is Trigger" box in the Inspector
Name it ScreamTriggerWall (fits the upcoming code)
Not add a script (in this case it's a C#-script)
Add the script to your FPS
Add the following code
void OnTriggerEnter(Collider col) { /* * If the name of the object is "ScreamTriggerWall" * - Then play a screaming sound * * NOTE: Figure out how to do that... */ if(col.name == "ScreamTriggerWall") { // play sound now } }
I will try it tomorrow, first time i see a C+ script :O
when i try to use it, it gave me an error. (1,9 = OnTriggerEnter)
Assets/Sound play.cs(1,9): error CS0116: A namespace can only contain types and namespace declarations
I tried some things but i can't get it to work. can someone make a full script so i know what i am doing wrong and how i can do it next time?
Your answer