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?

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


and you should be all set.

I would use an Animator go in to the “Windows” Tab, You will see Animation and Animator you will want the Animation for now.