- Home /
How do I make my walking animation transition to my running animation when I hold w and shift
My transition won't work it just stays on walking animation when I hold shift and w I'm only 13 so this is a struggle for me. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class running : MonoBehaviour { public Animator animator; private bool Running = false;
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.W))
{
Running = true;
animator.SetBool("isRunning", Running);
}
else if (Input.GetKeyUp(KeyCode.LeftShift) && Input.GetKeyUp(KeyCode.W))
{
Running = false;
animator.SetBool("isRunning", Running);
}
}
I am into the habit of enabling movement animations indepedantly without user input. this approach is almost manditory when dealing with networked games.
you can enable a "run" animation based on how much position difference you have between frames. its easier than tracking the buttons!!!
then you can use the same approach for enemy AI, network players and player without worry
Answer by gw_unity · Sep 13, 2018 at 02:01 AM
@jadd661 Well now when I stop holding down it does not go to idle
Sorry, you will need to use the Unity input manager(Edit --> Project Settings --> Input). Add a "Sprint" with the positive key as "left shift" then try:
void Update()
{
if (Input.GetButton("Sprint") && Input.GetAxis("Vertical") != 0f)
{
Running = true;
}
else
{
Running = false;
}
animator.SetBool("isRunning", Running);
}
Answer by jandd661 · Sep 13, 2018 at 01:20 AM
Input.GetKeyDown()
/ Input.GetKeyUp()
only returns true on the frame it was pressed/released. If you you are holding down "W" key, then pressed "LeftShift" (or released), none of the code above will fire because the key presses did not happen on the same frame. Use Input.GetButton()
as it stays true while it is pressed.
Your answer
Follow this Question
Related Questions
Transition Between Regular Animations and Ragdoll Physics 3 Answers
It is possible to make live transition between scenes? 1 Answer
How do you animate/transition a jogging character to a stop believably? 1 Answer
How to get character animations to transition? Why isn't this working? 0 Answers
Mecanim transition condition can't have both bool and trigger? 0 Answers