animation plays on button press?

Hi I’m New to Unity and I’ve been watching a tutorial on how to get a melee system working. The code works, but since his version of Unity is older than mine (the latest version) the animation constantly plays and I don’t know how to make it play when you click. I’ve tried using Play.animation() but apparently its not supported anymore. Here is my code:

#pragma strict

var TheDamage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;

function Update ()
{
if(Input.GetButtonDown("Fire1"))
  {
  //Here is where I need to know how to play the attack
      GetComponent("Attack");

   var hit : RaycastHit;
   if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit))
   {
    Distance = hit.distance;
    if (Distance < MaxDistance)
    {
     hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
    }
   }
  }

Any and all help is appreciated.

Not sure if this helps, but I was also a bit stuck with the whole animation thing. It was, and still is, a bit frustrating, but I was able to look up a few things. You want to call an animation when you click a key, right? Well, I’m sort of there. However, I’ll warn you in advance that I’m using Csharp(Sorry, never liked Unityscript).

So far though, I understand that animation.Play() isn’t used anymore(It took me forever to figure it out). If you can, then try this:

Void Start() {

//This should play the entire animation one time and stop it.

//NB: This will apply to ALL your animations in the Animation component.

GetComponent().Wrapmode = Wrapmode.Once
}

void Update() {

// I’m assigning the call command to the key “J” so that when I click “J”, my character attacks.

//You can use whatever key you’d like.

if ( Input.GetKeyDown(Keycode.J) {
GetComponent().Play(“Attack”);
}
}

Hope this helps. I’m still knew myself, so I apologize in advance if this isn’t what you were looking for. Good luck! :slight_smile:

I forgot to mention that when you use Wrapmode.Once, it will play the animation once every time your click. It will play the entire animation over and over again if you hold the required key.

Thanks dude so much for the help, I’ll see if I can code it in later sorry for the late response - I had to go out

I’ve tried the equivalent for javascript, it doesn’t work

GetComponent(“Attack”);

That code returns an error saying:

Operator ‘<’ cannot be used with a left hand side of type ‘function(System.Type): UnityEngine.Component’ and a right hand side of type ‘System.Type’.

I would use an Animator component on your gameobject instead of animation then create an animator controller. On the animator state machine you create a trigger parameter named “Attack”;
You then make a transition between AnyState and Attack state with a trigger condition on Attack trigger, and you make another transition from Attack state to exit without condition.

Then in your script you declare a private animator variable:

var anim : Animator;

In Start function you get the Animator component:

anim = GetComponent (Animator);

And when you need to play the animation:

anim.SetTrigger ("Attack");