- Home /
Changed the script completely. No longer require an answer.
Idle Animation Won't Play
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!!
Answer by Priyanka-Rajwanshi · Apr 18, 2018 at 04:50 AM
@Fibonacci_0_1_1 Your first condition would be true when you release W A S D simultaneously. For your query, remove the first if condition and place this condition at the end. I have also added a condition that the idle animation is currently not playing.
else if (!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D) && animationComponent.GetCurrentAnimatorStateInfo(0).shortNameHash != idleHashId)
{
Debug.Log("idle ANIME");
animationComponent.SetTrigger(idleHashId);
}
Follow this Question
Related Questions
Animation layer naming convention between enemies and player 0 Answers
How do i orginize 8+ movement animations? 1 Answer
Play simple animations on demand 1 Answer
making an arrowgrab animation play when attackdelay = 0. 0 Answers
Animation begins at start of Gameplay before colliding with trigger 2 Answers