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

Animation Script Problem! (Help)

Discussion in 'Scripting' started by Deshalsky, Dec 29, 2013.

  1. Deshalsky

    Deshalsky

    Joined:
    Nov 16, 2013
    Posts:
    57
    Hi there!
    I'm not so good at scripting so I got some problems..

    I have put 3 animations on a GameObject;
    Idle
    Walk
    Sprint

    I want the "Idle" animation to play whenever I go into PlayMode.
    I want the "Walk" animation to play when I press "W,S,D,A" on my keybord
    I want the "Sprint" animation to play when I press "Left Shift" on my keybord

    My "Idle" animation works fine...Except it never stops, Even if I play a new animation.
    When I press "W,S,D,A" nothing happens.
    And the same thing with my "Left Shift".

    I have made a script that should work:

    Code (csharp):
    1. #pragma strict
    2.  
    3. function Start ()
    4. {
    5.  
    6. }
    7.  
    8. function Update ()
    9. {
    10.  
    11. //Button Down
    12.  
    13. if (Input.GetButtonDown("Horizontal") || Input.GetButtonDown("Vertical"))
    14. {
    15.        animation.Play("Walk", PlayMode.StopAll);
    16.        animation.Stop("Idle");
    17. }
    18.  
    19.       if (PlayMode)
    20.       {
    21.             animation.Play("Idle");
    22.       }
    23.  
    24.             if (Input.GetButtonDown("Sprint"))
    25.            {
    26.                   animation.Play("Sprint");
    27.            }
    28.  
    29. //Button Up
    30.  
    31. if (Input.GetButtonUp("Horizontal") || Input.GetButtonUp("Vertical"))
    32. {
    33.       animation.Stop("Walk");
    34.       animation.Play("Idle");
    35. }
    36.  
    37.       if (Input.GetButtonUp("Sprint"))
    38.       {
    39.             animation.Stop("Sprint");
    40.       }
    41. }
    Thanks for you help! :D
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,851
    Have you actually defined input buttons named "Horizontal," "Vertical," and "Sprint"? These are nonstandard.

    Also, as for your script, it's working too hard. When I want to play an animation, I simply call animation.CrossFade — no need to stop the previous animation, or anything of the sort. So, back up... remove the script entirely, and make sure your character idles properly (it can do this without any script at all, provided idle is the first animation defined on it, and you have checked the "Play Automatically" checkbox).

    Then add a little script with this in its Update method:

    Code (csharp):
    1.   if (Input.GetKeyDown(KeyCode.W)) {
    2.       animation.CrossFade("Walk", 0.3f);
    3.   }
    Now test again. Your little guy should walk when you press the W key. Check at this point that your walk animation is properly set to loop; he should walk forever.

    Then you could start adding more logic to keep track of what state the character is in (idle, walking, running). The input checks should only update the state; and then you use the state (or state changes) to CrossFade to the appropriate animation.
     
  3. Deshalsky

    Deshalsky

    Joined:
    Nov 16, 2013
    Posts:
    57
    Thanks alot! :D
    But I got 1 Question...What does "CrossFade" do?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,851
    It plays the named animation, with a brief blend (I tend to use 0.3 seconds) from the currently playing animation. See the documentation for more details; Unity's docs are, in most places, really amazingly good.
     
  5. Deshalsky

    Deshalsky

    Joined:
    Nov 16, 2013
    Posts:
    57
    Okey, I've made a script for my animations, But how do I make it so the animations stop and go back to where it was before the animations started?