I have a unity script which controls my animations and sets triggers to play each of those animations.
The triggers are called using W, A, S or D and each of those play animations of jogging forward/back and strafe left/right. When W, A, S and D (all four of these keys) are up, i call my idle animation but it doesn’t play. When the game starts, my player is in idle state, but when i press any of the four keys (W, A, S or D) the state of that key plays forever even once GetKeyUp is true. I’m assuming something is wrong with my conditions for the idle state as it never becomes true, I’m just curious as to why so i can fix it. I’ve tried many different arrangements of the code and the the animator transitions just in case but nothing seems to work.
I’m trying to have it so that is all keys (W, A, S and D) are up, then idle animation will play.
Here is the animation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player_animation_control : MonoBehaviour {
Animator animationComponent;
int jogHashId = Animator.StringToHash("Jog");
int jogBackwardsHashId = Animator.StringToHash("JogBackwards");
int strafeLeftHashId = Animator.StringToHash("StrafeLeft");
int strafeRightHashId = Animator.StringToHash("StrafeRight");
int idleHashId = Animator.StringToHash("Idle");
void Start()
{
animationComponent = GetComponent<Animator>();
}
void Update()
{
//IDLE STATES ________________________________________________
if (Input.GetKeyUp(KeyCode.W) && Input.GetKeyUp(KeyCode.S) && Input.GetKeyUp(KeyCode.A) && Input.GetKeyUp(KeyCode.D))
{
Debug.Log("idle ANIME");
animationComponent.SetTrigger(idleHashId);
}
//ACTION STATES ________________________________________________
else if (Input.GetKeyDown(KeyCode.W))
{
Debug.Log("jog ANIME");
animationComponent.SetTrigger(jogHashId);
}
else if (Input.GetKeyDown(KeyCode.S))
{
Debug.Log("jog back ANIME");
animationComponent.SetTrigger(jogBackwardsHashId);
}
else if (Input.GetKeyDown(KeyCode.D))
{
Debug.Log("strafe right ANIME");
animationComponent.SetTrigger(strafeRightHashId);
}
else if (Input.GetKeyDown(KeyCode.A))
{
Debug.Log("strafe left ANIME");
animationComponent.SetTrigger(strafeLeftHashId);
}
}
}
thank you for your help its greatly appreciated!!