Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

animation plays on button press?

Discussion in 'Animation' started by Sir-Magic, Jul 22, 2015.

  1. Sir-Magic

    Sir-Magic

    Joined:
    Jul 20, 2015
    Posts:
    50
    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:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var TheDamage : int = 50;
    4. var Distance : float;
    5. var MaxDistance : float = 1.5;
    6.  
    7. function Update ()
    8. {
    9. if(Input.GetButtonDown("Fire1"))
    10.   {
    11.   //Here is where I need to know how to play the attack
    12.       GetComponent("Attack");
    13.  
    14.    var hit : RaycastHit;
    15.    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit))
    16.    {
    17.     Distance = hit.distance;
    18.     if (Distance < MaxDistance)
    19.     {
    20.      hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
    21.     }
    22.    }
    23.   }
    24.  
    Any and all help is appreciated.
     
  2. Farai

    Farai

    Joined:
    Mar 20, 2015
    Posts:
    6
    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<Animation>().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<Animation>().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! :)
     
  3. Farai

    Farai

    Joined:
    Mar 20, 2015
    Posts:
    6
    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.
     
  4. Sir-Magic

    Sir-Magic

    Joined:
    Jul 20, 2015
    Posts:
    50
    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
     
  5. Sir-Magic

    Sir-Magic

    Joined:
    Jul 20, 2015
    Posts:
    50
    I've tried the equivalent for javascript, it doesn't work

    GetComponent<Animation>("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'.
     
  6. jimmikaelkael

    jimmikaelkael

    Joined:
    Apr 27, 2015
    Posts:
    783
    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:
    Code (csharp):
    1. var anim : Animator;
    In Start function you get the Animator component:
    Code (csharp):
    1. anim = GetComponent (Animator);
    And when you need to play the animation:
    Code (csharp):
    1. anim.SetTrigger ("Attack");
     
    Last edited: Jul 28, 2015