Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make bool trigger an animation in blend tree

Discussion in 'Animation' started by Chumzy_01, May 19, 2020.

  1. Chumzy_01

    Chumzy_01

    Joined:
    Apr 26, 2020
    Posts:
    3
    Please, I'm a bit confused rn.
    I have a blend tree with idle, walk, run and Sprint animations.
    I've written a good script for player movement, but just for idle-run transmission. I'd want to make the Sprint animation work when I press leftShift with directions. Following a tutorial, I added a new input for Sprint, created a new bool for animation and now, how can I set that bool to be for that particular sprint animation in that blend tree?
    Should I remove and make it a single animation or is there a way I can still set it in that blend tree before writing a script for it?
     
  2. TheRothius

    TheRothius

    Joined:
    Jan 25, 2012
    Posts:
    3
    I've been looking for this answer and figured out a way for me. I set up a blend tree in between a walk blend tree and a run one. Set a parameter with speed to switch between the two. In the animation script I set shift to set this speed value to 1 or 0. That lets it switch between the two blend trees with a faked boolean. Not sure why they made a boolean parameter but you can't hard set it as a motion parameter. It's probably not the correct way but it worked for me.

    void Update()
    {
    animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
    animator.SetFloat("Vertical", Input.GetAxis("Vertical"));
    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
    animator.SetFloat("Speed", 1f);
    }
    if (Input.GetKeyUp(KeyCode.LeftShift))
    {
    animator.SetFloat("Speed", 0f);
    }
    }

    upload_2021-1-23_10-11-29.png

    EDIT
    The above won't have a transition between the two run and walk states. I fixed this below by slowly transitioning the speed variable. Just leaving this here incase anyone wanted this fixed too.

    public float currentSpeed = 0f;
    public int transitionSpeed = 8;
    private Animator animator;
    // Start is called before the first frame update
    void Start()
    {
    animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
    animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
    animator.SetFloat("Vertical", Input.GetAxis("Vertical"));
    animator.SetFloat("Speed", currentSpeed);

    if (Input.GetKey(KeyCode.LeftShift) && currentSpeed <= 1f)
    {
    currentSpeed += 0.1f * Time.deltaTime * transitionSpeed;
    }
    else
    {
    if (currentSpeed >= 0f)
    {
    currentSpeed -= 0.1f * Time.deltaTime * transitionSpeed;
    }
    }

    }
     
    Last edited: Jan 23, 2021
    hjkhjk likes this.
  3. hjkhjk

    hjkhjk

    Joined:
    Jun 16, 2016
    Posts:
    1
    Thank you, this is a nice little trick!
     
  4. Charlesm9

    Charlesm9

    Joined:
    Jul 22, 2021
    Posts:
    1
    Blend Trees are used for allowing multiple animations to be blended smoothly by incorporating parts of them all to varying degrees. The amount that each of the motions contributes to the final effect is controlled using a blending parameter, which is just one of the numeric animation parameters.



    My Balance Now
     
    Last edited: Jul 23, 2021
  5. njmcdirect

    njmcdirect

    Joined:
    Oct 15, 2021
    Posts:
    1
    To start working with a new Blend Tree, Right-click on empty space on the Animator Controller Window. Select Create State > From New Blend Tree from the context menu that appears. Double-click on the Blend Tree to enter the Blend Tree Graph.
    njmcdirect
     
    Last edited: Oct 16, 2021
  6. Maikikiii

    Maikikiii

    Joined:
    Mar 17, 2022
    Posts:
    1