How do i make my player go from walk animation to run animation using blend trees?
I need help going from the walk animation to run animation with blend. when i press left shift the character walks faster but when i click left shift i also want the run animation to work, this is my script & blend tree
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private float currentMaxSpeed = 0;
[SerializeField]
private Animator anim;
[SerializeField]
private float walkSpeed;
[SerializeField]
private float runSpeed;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
if (Input.GetKey(KeyCode.LeftShift))
{
currentMaxSpeed = runSpeed;
}
else
{
currentMaxSpeed = walkSpeed;
}
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
anim.SetFloat("Horizontal", movement.x);
anim.SetFloat("Vertical", movement.y);
anim.SetFloat("Magnitude", movement.magnitude);
transform.position = transform.position + movement * Time.deltaTime * currentMaxSpeed;
}
}
[1]: /storage/temp/133952-blend-tree.png
Answer by wolfkillshepard · Feb 28, 2019 at 09:40 PM
The way I have successfully used blend trees for a run animation was to use horizontal input from the left stick. Then I create a simple blend between walk and run(you can have alot more animation blends then 2) that only uses horizontal input to change between the blends.
I simply transition from idle to the run/walk blend when there is horizontal movement.
this is what my run/walk code looks like:
float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal"); // value is between -1 to +1
Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, rb2d.velocity.y);
rb2d.velocity = playerVelocity;
bool playerHasHorizontalSpeed = Mathf.Abs(rb2d.velocity.x) > Mathf.Epsilon;
float playerHorzSpeed = Mathf.Abs(rb2d.velocity.x);
myAnimator.SetBool("Running", playerHasHorizontalSpeed);
myAnimator.SetFloat("RunSpeed", playerHorzSpeed);