- Home /
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;
}
}
Answer by Skaster87 · Feb 28, 2019 at 05:08 PM
I'm going to assume this is a top down game with y and x being the ground plane and z is height?
The easiest solution I can suggest is a debug.log( movement ) to find the highest values that each speed produces, and then set the blend tree float to just above the walk speed range.
If you always want the run to trigger when the shift key is pressed, I would just assign the run speed value if the shift key is pressed in the update loop.
float speed = 1;
void Update()
{
if (Input.GetKey(KeyCode.LeftShift))
{
speed = 2;
}
else
{ speed = 1;}
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
anim.SetFloat("Horizontal", movement.x * speed);
anim.SetFloat("Vertical", movement.y * speed);
anim.SetFloat("Magnitude", movement.magnitude);
transform.position = transform.position + movement * Time.deltaTime * speed;
}
I would then use a single 2d blend tree with the walk animations and the run animations using x & y,
Answer by gagannagpal101 · Feb 28, 2019 at 06:11 PM
i did increase the run speed to 1.1 in blend tree and replaced that in my script but then the left, down animation stops working and when i release left shift it still walks fast, and if i hold it down longer it increases the speed, even letting go will keep that speed, have you got discord or skype?
Here is a clip of the problem: https://ufile.io/9ca8q
Here is a copy of my game: https://ufile.io/2gf5i @Skaster87
I updated the code to address your issue, but you'll need to address each animation in the blend tree. @gagannagpal101
Your answer
Follow this Question
Related Questions
idle > walk > run animation 1 Answer
Enemy animtion and movement 0 Answers
Locomotion System (By Rune Skovbo Johansen) 1 Answer
why my Animation "Run" wont work 1 Answer
Lerpz ThirdPersonPlayerAnimation bug - running at walking speed 1 Answer