Swordswing from blender first person

Hi I am new to Unity
I have made a sword in blender and I have made swing animation for it in blender…
When i put the sword in the world it do the animation …and when I attach the sword the the fps controller i can see the sword …etc… But how do i get the sword to do the animation when i press left mouse key? I cant seem to get it done …I have also tried many others keys …but i keep getting error messages like: Type UnityEngine.Component' does not contain a definition for Play’ and no extension method Play' of type UnityEngine.Component’ could be found. Are you missing an assembly reference?

this is my code…

if (Input.GetKeyDown(“Fire1”))
{
animation.Play(“name of animation”);

}
}
}

please help?

Use the animator instead, Its alot easier and more organized.

And the script should look like this:

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {

    Animator anim;

    void Start ()
    {
        anim = gameObject.GetComponent<Animator> ();
    }
 
    void Update ()
    {

     //The animation will be played once if you one-click, And it will continually play if you hold it.
        if (Input.GetKey (KeyCode.Mouse0))
        {
            anim.SetBool ("Swing", true);
        }
        else
        {
            anim.SetBool ("Swing", false);
        }
 
    }
}

Create an Animator and attach both the Animator and the Example script to your FPSController.
Open the Animator (DoubleClick), Drag and drop your animations (In this method you need an Idle State aswell).
(1) Click on paramenters and add a bool named “Swing”.
(2) Click on the arrow facing up that connects the “Idle” to the “Swing” and in the inspector uncheck “Has Exit Time”

Hope I helped :slight_smile:

1 Like