- Home /
How do I make a sword swing?
Hello everyone. First of all, I am a noob when it comes to Unity and a lot of JavaScript and most of C#. I know some Javascript, but I am still very stuck.
I have an animation on my character's arm and the sword which are attached. Now, all I need to figure out is how to make it so that when the left mouse button is clicked, the animation plays. Everything I try either doesn't do anything at all, or says "there is no 'animation' attached to the gameobject but a script is trying to access it". I would greatly appreciate it if the script was in Javascript, which I am more comfortable with. Any Help?
This is queued for closing.
There are many examples of this already. Please post what you've got.
Answer by Le-Capitaine · Apr 27, 2014 at 07:43 AM
First you need your character to have an Animator component. That component will reference what's called an animation controller, which is long story short a state machine. There, you'll drag and drop your animations and create transitions between them; in your case, you'll need a trigger, which we'll call "Swing", which can be added on the bottom left of the window. In the script, you'll declare a public Animator...
public var myCoolAnimator : Animator
and drag the animator component to the window that appears in the inspector after you save the script. Finally, somewhere in your script, you'll just have to call
myCoolAnimator.SetTrigger("Swing");
and you should be all set.
Thank you, but for some reason it is still saying "there is no 'animation' attached to the gameobject but a script is trying to access it". I'm very confused.
Answer by GoGamingJoe · Apr 27, 2014 at 09:04 AM
I would use an Animator go in to the "Windows" Tab, You will see Animation and Animator you will want the Animation for now.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143654im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Update Parent/ Children From Script? 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
How to display GUI in sequence in trigger 3 Answers
Animation Not Playing 1 Answer