- Home /
why my Animation "Run" wont work
Hello! Good am/pm, im new in coding and i have an issue on my script because when i press left shift and w the run animation are mixing with walk animation and sometimes the run animation will instantly will not work and i have no idea on how to fix it! i hope somebody can help me, Thanks!
{
private Animator mAnimator;
// Start is called before the first frame update
void Start()
{
mAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
bool IsWalking = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow);
mAnimator.SetBool("IsWalking", IsWalking);
if(Input.GetKeyDown(KeyCode.Space))
{
mAnimator.SetTrigger("IsJumping");
}
if(Input.GetKeyDown(KeyCode.W) && Input.GetKeyDown(KeyCode.LeftShift))
{
mAnimator.SetTrigger("IsRunning");
mAnimator.SetBool("Iswalking", false);
}
}
}
Answer by Llama_w_2Ls · Aug 17, 2020 at 09:03 AM
Somewhere in your code, you should redefine IsWalking and maybe set it as a method. This is because IsWalking is always true whenever you press any key but is set to false when pressing forward and left shift, but the bool runs again and resets it back to true, and back to false vice versa. You should be defining your IsWalking and IsRunning like this:
public bool IsRunning = false;
public bool IsWalking()
{
float x = Input.GetAxis("Horizontal"); // Includes keys: 'a', 'd', 'left arrow', 'right arrow'
float y = Input.GetAxis("Vertical"); // Includes keys: 'w', 's', 'up arrow', 'down arrow'
if (Mathf.Abs(x) > 0 || Mathf.Abs(z) > 0) // If either one of the keys are pressed (if im moving in any direction)
{
return true; // IsWalking = true
}
else if ((Mathf.Abs(x) > 0 || Mathf.Abs(z) > 0) && Input.GetKey(KeyCode.LeftShift)) // If either one of the keys are pressed (if im moving in any direction) AND Left shift is pressed
{
IsRunning = true; // IsRunning = true
return false; // IsWalking = false
}
else //If no directional keys are pressed (not moving)
{
IsRunning = false; // IsRunning = false
return false; // IsWalking = false
}
}
If Input.GetAxis("Horizontal") is not automatically set to 'a', 'd', 'left arrow' and 'right arrow', then you can change it in the editor -> project settings -> Input Manager -> Horizontal. Hope it helps! @MyUserNameIsWhat ... What is your username btw?