Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

How to mix animations of idle, walking and running with a smooth transition ?

Discussion in 'Animation' started by KENDIl, Nov 22, 2019.

  1. KENDIl

    KENDIl

    Joined:
    Nov 13, 2019
    Posts:
    13
    I work with animations using the Blend Tree.
    I want to add running animations using LShift. How can i do this?
    My animation script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AnimatorScript : MonoBehaviour
    6. {
    7.     Animator animator;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         animator = GetComponent<Animator>();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         float v = Input.GetAxis("Vertical");
    19.         float h = Input.GetAxis("Horizontal");
    20.         animator.SetFloat("vertical",v);
    21.         animator.SetFloat("horizontal", h);
    22.     }
    23. }
    24.  
    My blend tree:
    Снимок.PNG
     
    Last edited: Nov 22, 2019
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @KENDIl,

    First off, why don't you name your animations :) You can do that in animation properties in Inspector, just name the clips. That will make your life easier... And also let others to understand how you have configured your blend tree etc. Proper naming conventions are important in any kind of organized project.

    When you set a value that blends the idle-sneaking-walking-running or whatever you have, take that Shift key into consideration there? i.e. if you set your value based on vertical input, don't get it go past certain value without the Shift key pressed.

    I have implemented this myself something very simply like this:
    Code (CSharp):
    1. if (isRunning == false) {
    2.   forwardMovement *= 0.7f;
    3. }
    4. // Else, forwardMovement will be 1.
    And in this imaginary scenario my animation would be configured so that running blend starts only after forward movement is over 0.7.
     
  3. KENDIl

    KENDIl

    Joined:
    Nov 13, 2019
    Posts:
    13
    I did as you said, and now my character is always running.
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well you set something wrong. Please think what I said and try to implement it to your specific case. I just gave an example. That's one approach that works fine in my opinion.
     
  5. KENDIl

    KENDIl

    Joined:
    Nov 13, 2019
    Posts:
    13
    I wrote like this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AnimatorScript : MonoBehaviour
    6. {
    7.     Animator animator;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         animator = GetComponent<Animator>();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         float v = Input.GetAxis("Vertical");
    19.         if(Input.GetKey(KeyCode.LeftShift))
    20.         {
    21.             v *=1f;
    22.         }
    23.         else
    24.         {
    25.             v *= 0.5f;
    26.         }
    27.         animator.SetFloat("vertical", v);
    28.  
    29.         float h = Input.GetAxis("Horizontal");
    30.         animator.SetFloat("horizontal", h);
    31.     }
    32. }
    33.  
    But now the animation changes to running sharply, without a smooth transition
    And yes, here is my renamed blend tree))
    Снимок.PNG
     
    Last edited: Nov 22, 2019