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. Dismiss Notice

Chaining mechanim animations together?

Discussion in 'Scripting' started by Macro, Jul 13, 2014.

  1. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    45
    Hopefully a common use-case and simple question.

    Assuming I have an animation controller which has the animations punch_1, punch_2, kick_1, kick_2 and I have buttons on the pad for punch and kick. I want the moves to basically flow into each other, so when they press the punch button it will play the punch animation, then when they press the punch button again it would go into punch 2 animation ONLY after a certain point in the animation, otherwise they player could constantly just flit between the animations.

    So currently I have 2 booleans isPunching, isKicking and then an int attackState for the state of the attack, as technically you could go punch, kick, kick, punch which would link from punch_1 -> kick_2 -> kick_1 -> punch_2.

    So I was planning to set the bool when a key was pressed, but I need to know how long the animation will take so I can see how long I need to wait to check for the next button press. The other alternative is to just manually call the play method and play the animations explicitly rather than setting it via variables.

    So is there a simple way to get this scenario working, the acceptance criteria would be:

    Given I press the punch button
    And the player is currently playing the punch_1 animation
    When I press the punch button before the animation cutoff point
    Then the player should NOT break the punch animation

    Given I press the punch button
    And the player is currently playing the punch_1 animation
    When I press the punch button after the animation cutoff point
    Then the player should then link into the punch_2 animation

    Given I press the punch button
    And the player is currently playing the punch_1 animation
    When I press the kick button after the animation cutoff point
    Then the player should then link into the kick_2 animation

    If the player does not press a button or presses it before the cutoff point it should just revert back to the idle state.

    Hope this all makes sense, I just want to see how others solves this sort of problem.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    What if you define a refractory period parameter and use it in transitions? So the transition condition might be something like "(punchButton is True) AND (time Greater than refractoryPeriod)". Or, instead of refractoryPeriod, you could manage a Boolean parameter, and use it something like: "(punchButton is True) AND (canTriggerNewMoves is True)".
     
  3. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    45
    How do I define when that refactoryPeriod is over or when that canTriggerNewMoves should be set to true. As I basically want to avoid having to hardcode timespan values into the game logic to deal with animations, as lets say punch_1 may last for 2 seconds but kick_2 may last for 3 seconds, so the period needed to wait is different.

    Originally I was just wanting to wait for the animation to finish, and in that window I would just capture what the last attack button being pressed was (if any) then transition to the next one, however at the time I remember people saying there was some bug or missing functionality with Mecanim so you could not find out how long an animation would play for.

    I guess its difficult for me to wrap my head around how to manage timings etc as with 2d or vertex based animation its simple as you know to wait for the frames to end, but in the bone based world everything is interpolating between different animations so I am just a bit confused how to know exactly when to to trigger things...
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    For each move, you can add an Animation Event at the point that canTriggerNewMoves should be true. This event would just call a method to set canTriggerNewMoves=true.
     
  5. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    45
    Isnt animation events for legacy animations not for mecanim?
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    They were introduced in Unity 4.3 Here's a tutorial:



    The first half is about Pro-only curves. The last half is about Pro/Free events.

    If you're not able to upgrade to 4.3+, you can use G1NurX's excellent Event System for Mecanim.
     
  7. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    45
    No version constraints thanks for the info will give it a watch!