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

Not sure how to tackle alternate animations

Discussion in 'Animation' started by Zorugh, Sep 11, 2022.

  1. Zorugh

    Zorugh

    Joined:
    Feb 25, 2017
    Posts:
    3
    Hello, I'm making a 2D platforming game - with the option of holding down left mouse button to extend your arm and point it at the cursor. However, during this phase of extending the arm, I have a new set of animations to replace the current ones - and I have no idea how to implement this. I've done a lot of googling, but so far no luck. I tried expanding my Enum of "States" (such as running, jumping etc) to include the new ones like "RunningNoArm" and "JumpingNoArm" - then adding a whole bunch of IF/ELSE statements, but had no success with that, and just broke the code entirely. And if I had managed to get the correct states anyway, I would then somehow have to add connections between every single animation in the Animator? Surely there's a better way

    Thank you in advance;
    A complete beginner^^

    Example Code:
    Code (CSharp):
    1. private void VelocityState()
    2.     {
    3.  
    4.  
    5.         if(state == State.jumping)
    6.         {
    7.             if(rb.velocity.y < .1f)
    8.             {
    9.                 state = State.falling;
    10.  
    11.             }
    12.  
    13.         }
    14.  
    15.         else if (state == State.falling)
    16.         {
    17.             if (coll.IsTouchingLayers(ground))
    18.             {
    19.                 state = State.idle;
    20.  
    21.             }
    22.  
    23.         }
    24.         //If an absolute number of movement on x axis is greather than the absolute lowest possible, then use running animation
    25.         else if (Mathf.Abs(rb.velocity.x) > 2f)
    26.         {
    27.             //Moving
    28.             state = State.running;
    29.  
    30.         }
    31.  
    32.  
    33.         //otherwise, use idle animation
    34.  
    35.         else
    36.         {
    37.             state = State.idle;
    38.         }
    39.        
    40.     }
    41. }
     
  2. Zorugh

    Zorugh

    Joined:
    Feb 25, 2017
    Posts:
    3
    Update: I have now found the Asset called Animation Override Controller. So I've made one of those, and applied the alternate animations to it - now I'm just struggling to figure out how to implement that one
     
  3. Zorugh

    Zorugh

    Joined:
    Feb 25, 2017
    Posts:
    3
    SOLVED - created two overrides, one for default animations, one for the new ones, and assigned the animator to the appropriate one during the right conditions
     
  4. tcshaw91

    tcshaw91

    Joined:
    Jul 26, 2018
    Posts:
    7
    so you could create a sub state machine for like "default" with your current states and transitions and then copy pasta it, call the copy "aim" and fill the states with the new clips, then just have an "isAiming" bool that transitions between the sub state machines...

    or honestly if it's a 2d platformer and you're already managing the state in your code, you could forgo state transitions and parameters entirely and simply call "animator.Play(state)" directly from code. Theres a youtube vid called "Escaping Animator Hell" where the guy describes how he implemented this technique and how it saved him a bunch of headache. Of course if you're using blend trees and layers and stuff you'd prolly wanna buy an asset called "Animancer" but if you're doing a 2d platformer you shouldn't need it.