- Home /
Animation not playing via collider and script (game just chugs instead)
Hello.
In an FPS game, I am trying to get a box to play an open animation whenever the player collides with it.
The box has ana animation that opens its lid (the lid is a child of the box). Whenever a copy of the asset is created, its animation will play automatically whenever the game loads, as expected.
I have the following script attached to the First Person controller:
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "chest")
{
hit.GameObject.animation.Play("box_open");
}
}
Next, I add a Box Collider to the box object (parent, not the lid) and disable "Play Automatically" in its Animation component. I then add a "chest" tag to it for the script.
When the player walks into the object, no animation is played and the game actually chugs while the player is in contact with the box.
I am definitely missing something here, just not quite sure what it is. Any assistance would be greatly appreciated.
Answer by meat5000 · Dec 06, 2013 at 07:02 PM
hit.GameObject.animation.Play("box_open");
GameObject typo
GameObject is a Type gameObject is an object
Does the box have a rigidbody?
Gah! That was totally it! I didn't notice that typo.
Thank you much for your help. :)
Answer by Rams · Dec 06, 2013 at 08:02 PM
You have to probably use animation events. Do as below process... 1)open animation editor(box_open) and select an animation event at starting and attach it to the method.code will be...->
take another var(for ex: hit1) of type ControllerColliderHit in the starting, assign hit to hit1(making reference). if(hit.gameObject.tag == "chest") call that method.
in the method definition hit1.gameObject.animation.play("box_open").