- Home /
How to access Animation using java script & attach it to the object ?
i have created the walking & idle animation for my fps player. i've added a java_script to access it. but, when i run it & press button(w), it stops & says that, the animation is not attached to the object.. when i drag the walking/idle animation & drop to that object, it creates an animation controller. again i run it, still same error. i can't find where to attach the animation ?????? thnx for help... @Adamcbrz @Olgo @whydoidoit @VildNinja @hollym16 @ Here's my java script:
function Update () {
if(Input.GetKeyDown("w"))
{
GetComponent.<Animation>().Play("walk_anim2", PlayMode.StopAll);
}
if(Input.GetKeyUp("w"))
{
GetComponent.<Animation>().Play("idle_anim2", PlayMode.StopAll);
}
}
GetComponent<Animation>()
or GetComponent<Animator>()
Is that not C# ? the OP did say JavaScript
Javascript isn't my default choice :P, I think they work in both cases otherwise it should be;
GetComponent(Animation)
or GetComponent(Animator)
Nope, it's on the correct object. @vintar anyway, actually i want to acces the walk animation for my FPS when a particular key is pressed. so, can u tell me a simple step by step procedure ? ? ? newbies here !!!
@hexagonius
i've added the screenshot.
i had once added the animation component too. but, that was also not working. anyway, what exactly i have to do right now? @hexagonius
Use comment and not answer when you don't have an answer!!! Converted to comment.
Answer by hexagonius · Jan 06, 2016 at 07:28 AM
first, it is GetComponent(Animator)
second, it works like a state machine with transitions where you define the conditions in those white arrows. In your case this would be a speed float value higher than 0 for entering walking and 0 for going back to idle. the animator uses SetFloat for this.
see the docs for a description of the Animator about how to use this and read this carefully to get an understanding how everything works
Your answer
![](https://koobas.hobune.stream/wayback/20220612053558im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with attacking, and death animation. Unity 5 javascript 1 Answer
How to loop unity?JS 3 Answers
Animation Played when key pressed Help 1 Answer
Animation depending on the Int value 0 Answers