- Home /
How to play an animation using a trigger???
Hey!
Trying to figure out how to do this:
I want an animation to be played when my player enters a trigger. As a trigger I used a box and enabled isTrigger, I also tagged the FPS controller as "Player" then I made the script below...
This is my code right now:
function OnTriggerEnter (collision : Collider) {
if (collision.gameObject.tag == "Player"){
animation.Play ("airplane");
}
}
Dosen`t seem to work... Then should I attach this script to the trigger or....???
Many thanks in advance! :)
Answer by Peter G · Apr 19, 2010 at 12:04 AM
There could be a couple of issues:
Make sure your animation is called "airplane" not "Airplane" or "airPlane".
Make sure you used bone based animation to animate the player.
Or what appears to be the most likely error is that you are calling the animation "airplane" on the object this script is on when you want to be calling it in the player.
To fix add this script to the trigger (which cannot move unless you add a kinematic rigidbody) and change your code to this.
if (collision.gameObject.tag == "Player"){
collision.animation.Play ("airplane");
// we want the player's animation to play, not the trigger's.
}
this script seems to work for the opposite of what the inquirer asked, this script moves the player but we wanted the "object" to move not the player.. thanks anyways.
No, I can not tell for sure, but it appears to me that there is an empty trigger (which would not have any animations on it) that when the player enters triggers the player's animation.
Answer by khalil · Apr 20, 2010 at 04:30 AM
If you haven't figured it out already here is a strong solution.
First animate your object that you want to move.
then give a name to your new AnimationClip.
then attach this Animation to the object itself (uncheck "plays automatically")
We will use a door opening and closing all on one animation (no need to use 2 separate animations for a simple door function.) in this example.
then say:-in Java-
var (dooropenandclose) : AnimationClip; //this is your animation clip.
var (door) : GameObject; //this is your game object that you want to move on trigger.
function OnTriggerEnter(){
animation.Play ("dooropenandclose");
}
then attach your script to your (box collider is triggered) Object (not camera). Then under the script's variables attach your Object to your script, and your AnimationClip to your script's variables. (you should be able to tab into these open variable slots after you attach your script to your game object if not, your script is not syntaxed correctly.)
enjoy. K
Answer by pacifictyphoon · Apr 17, 2012 at 05:04 AM
I believe I have followed the instructions to the T and still got this error:
The animation state dooropenandclose could not be played because it couldn't be found! Please attach an animation clip with the name 'dooropenandclose' or call this function only for existing animations.
Thanks!
Answer by martynator · Nov 26, 2013 at 07:01 PM
i'm getting unexpected token at 1,5 "(" i have no idea how to fix it.