- Home /
Animation plays on when colliding with a trigger
I want to make a it so when my player collides with my trigger (empty game object) it starts my animation of a table moving across the room.
I tried using this script
var (animation) : AnimationClip;
var (object) : GameObject;
function OnTriggerEnter(){
animation.Play ("animation");
}
but it doesn't seem to work.
I'm very new to scripting and any help would be appreciated!
Answer by Kleptomaniac · Apr 10, 2012 at 03:03 AM
I would suggest not calling you AnimationClip "(animation)" or "animation". The former because, well, brackets are bad syntax (and, besides, you don't seem to even use the variable in the script) and the latter because animation is already a class. Call it something like walk or something descriptive of what the animation actually does.
Same goes for object: I would suggest no brackets and better naming. Also, you don't use that variable anywhere in the script either.
Thirdly, to specify a trigger, you need to setup OnTriggerEnter in terms of a collider (e.g.
function OnTriggerEnter (other : Collider) {
and then I would check to see if the gameObject hit has a specific tag withif (other.collider.gameObject.CompareTag("MyEmpty")) {
Make sure you have an actual animation with the name "animation" (case-sensitive). Instead of doing a string look-up though, I would suggest you continue using an AnimationClip variable, and say the name of your variable is walk, play it by going
walk.Play();
Hope that helps! Klep