- Home /
Detect OnTriggerEnter with tags
Hello, I am wanting to detect if this game object has hit another game object tag ground if so I want to play and animation and I want to play a sound. Unfortunately I cannot find how to detect the grounds tag. I know this is simple, I am missing it.
Thanks for the help Daniel
PS: Also I would like to know if i am coding the correct way to play a different clip on the same audioSource? Alos would I use AudioClip or Audio as my var class type.
function OnTriggerEnter (gameObject.tag ="ground" : Collision) {
fallingAnimation.Play();
audio.Play("CrashSound");
}
Answer by markpdolby · Jul 02, 2013 at 11:15 PM
var audioClip : AudioSource;
function OnTriggerEnter(collider : Collider)
{
if(collider.tag == "ground")
{
fallingAnimation.Play();
audioClip.Play();
}
}
or this if you dont want to use triggers:
var audioClip : AudioSource;
function OnCollisionEnter(collider : Collision)
{
if(collider.gameObject.tag == "ground")
{
fallingAnimation.Play();
audioClip.Play();
}
}
In regards to playing audio I don't have much experience but I believe you need to reference a separate audio source for each sound you want to play and call .Play() on that audio source. Also make sure that you have your objects set up right so that they can detect collisions: http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
I deleted my answer, because this is correct and contains more information. +1
YES! One more thing if your wanting to play one multiple clips on the same audio source you need to do so like this :D var FingerOverSound : AudioClip; //$$anonymous$$A$$anonymous$$E sure this is an AudioClip! Not Audio source or audio only.
function On$$anonymous$$ouseEnter () {
audio.clip = FingerOverSound; // This is the essential part!
audio.Play();
}
Is there any performance difference between using trigger or collision, like if we are talking about this example?
This is not an answer. Please use the 'add new comment' button in the future.
Performance doesn't really come into it, it is more about what you are trying to achieve. Some examples of use are: Use a collision when two physical objects are co$$anonymous$$g into contact with each other and use triggers for when you want something to happen when an object enters a defined zone etc.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Speed Pill... Variable referencing and adjusting temporarily 3 Answers
How to Implement System.StringBuilder into JavaScript? 2 Answers
Changing material Color using RBG? 3 Answers
Has anyone got a Toggle key script? 3 Answers