- Home /
How to trigger animation on a different object?
I have a game where a player has to get through a maze without getting caught. I have the player tagged as "Player". When he walks into a collider and gets caught, i need a light to come on and play an animation called "Caught". The light is called "Spotlight" and the collider is called "Collision". I need the script for this and also i need to know if i should attatch this script to the player, the collider, or the spotlight. Thanks.
Answer by Cherno · Nov 18, 2013 at 01:10 AM
This should work, atach it to your "Collision":
//this object needs a collider, and the player object needs a collider AND a rigidbody!
//the player object's rigidbody can be set to IsTrigger so it isn't affected by physics.
//the Light Object needs to have it's intensity set to 0.0.
var LightObject : GameObject;//Drag your light object into this field in the inspector
function OnCollisionEnter(other : Collider)
{
if(other.tag == "Player")
{
LightObject.light.intensity = 1.0;
LightObject.animation.Play("Caught");
}
}
however, it is more effective to attach it to the player object. In that case, you just give the "Collision" a tag and let the OnCollisionEnter function check if it hit an object with that tag. The advantage is that you will only call OnCollisionEnter from one script instance (the player) instead of from every "Collision" in the level.
Your answer
Follow this Question
Related Questions
Problem with Reverse Animation 1 Answer
Trigger transition to another animation. 2 Answers
4.5.2 animation BUG: 2D collider colliding with 2d trigger!!! 0 Answers
Can't click gameobject when over another trigger? 1 Answer
Trigger via Collider 1 Answer