- Home /
How to play animation (C#)
I created an animation and animator components and putted the animation on the parameters and changed the animation to legacy and I wrote this..
if (Input.GetKey (KeyCode.W))
GameObject.Find("Object").GetComponent<Animation>().Play("AnimtionName");
but it didn't work.. I need help in this Any help would be useful, Thanks in advance
Answer by Danisuper · Jun 05, 2016 at 02:15 PM
You should use the animator,wich is automatically created when you make an animation for a gameobject. Then in your script you'll write
if (Input.GetKey (KeyCode.W))
GameObject.Find("Object").GetComponent<Animator>.SetTrigger("PlayAnimation");
Watch this tutorial about the animator for more infos. Hope it helped ;)
@Danisuper Sorry for the late response, I had some work to do. Anyway, I just tried this but it didn't work. I just have one question, Do I have to write the animation name or the animator name? after SetTrigger.
You must use the animator. The rectangles are the the animations,then you must link them with an arrow. In this arrow you must choose one or more conditions, wich can be triggers,bools,floats or ints. When the conditions occur the animation is played/changed depending on the structure of your animator,so in the GameObject.Find("Object").GetComponent.SetTrigger("a trigger" ); you must first write in the animator a trigger parameter then click on the arrow between your animations (the ones you want to change,for example idle animation--->walk animation),add a condition and click on the trigger you just created. So for example, you have and idle and a walk animation. To go from idle to walk you must first add a trigger parameter,give it a name,for example "walk" and link the rectangles of the animations with an arrow, Then in the arrow add a condition and set it to be the trigger you just created. now in your code if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W)) GameObject.Find("Object").GetComponent<Animator>.SetTrigger("walk");
` you must enter in SetTrigger the name of the trigger you just created wich is "walk" in this case,the condition we created in the animator is true so the there will the animation. Hope it helped you
@Danisuper Thanks, you really helped me, now I understand the animator tab a little bit more. But why do I have to use SetTrigger? bool should be easier, you can just make a bool parameter and make it play the walking animation when true, and play the idle animation when false. But it didn't work when I used bool. Can you please show me how to use the bool parameter, and the SetTrigger worked but I can't understand how it work.
Answer by pronobroy · Jun 25, 2016 at 05:35 AM
For play an animation first you should make an animator controller and then you can paly animation by script
animator anim; if (Input.GetKey(keycode.A) anim.setbool ("name of the animator state" , true )
note that 1st parameter is for name of state and 2nd one is for bool value.
Also you add trigger ,float value to play animation clip.
Answer by JacksonPeeven · Jun 05, 2016 at 02:15 PM
Hey @Ihabbalakho I would love to help! Okay so the issue with what you are doing is that you are attempting to directly modify the component. Unity doesn't like this and requires you to make a reference of the component first as a variable, and then play the animation from that variable.
Example:
var gameObjectAnim = GameObject.Find("Object").GetComponent<Animation>(); gameObjectAnim.Play("AnimationName");
I hope this helped! If you have any other questions I would be glad to help!
@JacksonPeeven Sorry for the late response, I had some work to do. Anyway, I have created a public variable and changed it to the object that I want animate but unfortunately it didn't work. and that doesn't change anything by the way because it works just like the GameObject.Find(); Thanks anyway.
Your answer
Follow this Question
Related Questions
Legacy animation not playing - no errors? 1 Answer
How do I play legacy animations properly in Unity C# 1 Answer
Legacy Animation doesn't work... 1 Answer
Animation Doesn't CrossFade At End 0 Answers