- Home /
How to right set the trigger on an object
Hello!
I'm creating the game "Bowling". There must be sound when the ball hits skittles. My idea to make it is to create skittles's clones and to make a trigger tick. But i think it is wrong. What is the best way to do it? How can i count skittles? How can i know, what skittles are knocked down to remove them from the scene? May can i assign trigger property to object without losing physics, collider, displaying?
Answer by aldonaletto · Nov 02, 2011 at 01:07 AM
You're right: if you transform the skittle collider in a trigger, it will fall through the floor. It's better to let it be a regular collider with rigidbody, and use OnCollisionEnter to detect when something hits the skittle. To know if a skittle is knocked down, verify it's up direction.
Do the following: import the skittle model, add a mesh collider to it and check Convex (or it will fall through the ground), and add a rigidbody. Add also an AudioSource, and set its clip property to the sound you want. Tag it "Skittle" (you must create the tag first in the Inspector). Finally, add the script below to the skittle and drag it to the Project panel to make it a prefab.
function OnCollisionEnter(col: Collision){ if (col.relativeVelocity > 0.2){ // if hit at some minimum velocity... audio.Play(); // play the sound } }
You can create the skittles using Instantiate, or just drag the prefab to the scene how many times you need.
To know which skittles are knocked down, you can get all skittles in scene in an array and verify its up direction, like this:
function CountKnockedDown(){ var skittles = GameObject.FindGameObjectsWithTag("Skittle"); for (var skittle in skittles){ if (skittle.transform.up.y < 0.97){ // if angle to ground < 75 degrees // skittle is knocked down: do whatever you want with it, // like counting points, destroying the skittle, etc. } } }
Your answer
![](https://koobas.hobune.stream/wayback/20220613030253im_/https://answers.unity.com/themes/thub/images/avi.jpg)