- Home /
Animator SetBool not changing to true C#
I'm quite positive I have done everything correctly however the Animator still doesn't want to set the parameters to true so the transition can take effect. If you would like me to post a photo of the animator I will however I assure you the parameters are setup correctly with the transitions and all spelled correctly.
Here I assign the hash and bool values. public KeyCode Forward; public KeyCode Backward; public KeyCode Left; public KeyCode Right;
public Animator PlayerAnimations;
public bool IsMoving;
public int moveHash;
public int attackHash;
public bool CanAttack;
void Start()
{
Forward = KeyCode.W
Backward = KeyCode.S
Left = KeyCode.A
Right = KeyCode.D
PlayerAnimations = GetComponent<Animator>();
moveHash = Animator.StringToHash("IsMoving");
attackHash = Animator.StringToHash("IsAttacking");
IsMoving = true;
CanAttack = true;
}
Then here I tell it if one of the 4 movement keys are hit switch the parameter for the moving transition to true, and likewise for the attack if the left mouse button is clicked
void Update()
{
//If one of the 4 movement keys are hit transition into moving animation or if not go back to idle
if (Input.GetKey(Forward))
{
transform.Translate(Vector3.forward * 5 * Time.deltaTime);
IsMoving = true;
PlayerAnimations.SetBool(moveHash, IsMoving);
}
if(Input.GetKey(Backward))
{
transform.Translate(Vector3.back * 5 * Time.deltaTime);
IsMoving = true;
PlayerAnimations.SetBool(moveHash, IsMoving);
}
if(Input.GetKey(Left))
{
transform.Rotate(-Vector3.up * 30 * Time.deltaTime);
IsMoving = true;
PlayerAnimations.SetBool(moveHash, IsMoving);
}
if(Input.GetKey(Right))
{
transform.Rotate(Vector3.up * 30 * Time.deltaTime);
IsMoving = true;
PlayerAnimations.SetBool(moveHash, IsMoving);
}
else
{
IsMoving = false;
PlayerAnimations.SetBool(moveHash, IsMoving);
}
//If the left mouse button is clicked transition into attack animation
if(Input.GetMouseButtonDown(0))
{
PlayerAnimations.SetBool(attackHash, CanAttack);
}
else
{
CanAttack = false;
}
}
Answer by saschandroid · Aug 01, 2016 at 07:29 PM
I didn't test if this solves you problem but you are using 'if/else' in the wrong way. With else in line 31 you are setting IsMoving = false every time you are not pressing the 'Right'-key.
Change your code to something like:
void Update()
{
IsMoving = false;
//If one of the 4 movement keys are hit transition into moving animation or if not go back to idle
if (Input.GetKey(Forward))
{
transform.Translate(Vector3.forward * 5 * Time.deltaTime);
IsMoving = true;
}
if(Input.GetKey(Backward))
{
transform.Translate(Vector3.back * 5 * Time.deltaTime);
IsMoving = true;
}
if(Input.GetKey(Left))
{
transform.Rotate(-Vector3.up * 30 * Time.deltaTime);
IsMoving = true;
}
if(Input.GetKey(Right))
{
transform.Rotate(Vector3.up * 30 * Time.deltaTime);
IsMoving = true;
}
PlayerAnimations.SetBool(moveHash, IsMoving);
//If the left mouse button is clicked transition into attack animation
if(Input.GetMouseButtonDown(0))
{
PlayerAnimations.SetBool(attackHash, CanAttack);
}
else
{
CanAttack = false;
}
}
Correction that actually was the issue for the moving one, can't believe I didn't think of that knowing how long ive been using if else statements.
Your answer
Follow this Question
Related Questions
UnityEngine.AnimatorStateInfo.nameHash' is obsolete: "Use AnimatorStateInfo.fullPathHash instead." 0 Answers
Animator HashToString returns 0 1 Answer
How to run String To Hash at compile time? 1 Answer
How to get Animator's Layer --> Sub-State --> State namehash? 2 Answers
2D Animation does not start 1 Answer