Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    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:
    2
    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. Rothius13

    Rothius13

    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
unityunity