Create Idle/Walk/Run Blendtree
Hey there,
I' m trying to setup the blendtree in mecanim for hours now. I just want a simple fade between the motions "Idle", "Walk" and "Run", if left shift is pressed. Currently my blendtree looks like this:
I don't know how to set the float properly for the transition from idle to walk and then from walking to running.
Thanks for your help!
Answer by Jessespike · May 27, 2016 at 08:38 PM
The BlendTree needs the float to be set between 0 and 1. So in your character controller script, lerp the float to 0 when Shift is not pressed, and lerp the float to 1 when shift is pressed.
Holding shift to move around is uncommon. Usually you would use Input.GetAxis or something relative like the velocity of the character to set the float. Especially for movement blendtrees that require different states, idle, walk, and run. Not sure how you plan on differentiating between walk and run, when shift can only be either pressed or not.
Maybe something like this:
Animator animator;
float speed = 0f;
// Update is called once per frame
void Update () {
if (animator == null) animator = GetComponent<Animator>();
if (Input.GetKey(KeyCode.LeftShift))
{
speed += Time.deltaTime;
}
else
{
speed -= Time.deltaTime;
}
speed = Mathf.Clamp(speed, 0f, 1f);
animator.SetFloat("Speed", speed);
}
I think the tutorials explain all of this in a better way: https://unity3d.com/learn/tutorials/topics/animation
First of all, thanks for your answer! Currently I'm indeed using Input.GetAxis to set the float. It's 0 when there's no input from vertical or horizontal axis and it is increasing to 1 the other way around. $$anonymous$$y problem is now the transition to "run". I'd like to smoothly blend between walk and run when left shift is held down. If the user releases the button, the animation should blend back to walk.
Your answer
Follow this Question
Related Questions
Animation sinks player into the ground 1 Answer
How do I modify animation parameters from script (C#) 1 Answer
Animator Position VS Script Vector 3 0 Answers
Why isn't my animation looping? 3 Answers