I have a very simple game running in Unity, and unfortunately, it is not working as expected.
The Context
I have the following setup:
(source: mazyod.com)
From any state, if an integer parameter direction
is equal to 1
, set animation player_move_e
. Same goes for 2
, 3
, 4
for the other directions. Once direction
is set to 0
, the idle animation associated with the direction is played.
An important thing to point here is, since I am using the "Any State" block, I must make sure that once the animation state changes to one of the move animation states, I reset direction
to sentinel value, like -1
, that keeps the animation state in the current state, and not change states to itself again.
The Problem
When the player is in the, say, move_n
state, and I quickly change the direction to the, say, move_e
(not giving it enough time to transition to idle), the animation state is stuck at the move_n
:(
How can I know if the transition happened, and I am not updating too often? Initially, I was updating direction
in FixedUpdate
, and then moved it to Update
, but it's the same issue in both methods.
Here is the logic I am seeing:
// MY CODE
animator.SetInteger(1); // Initially, it's move_e
...
// WITHIN UNITY (making it up)
Unity.UpdateStates(); // The number is read in the engine, and transition happens
...
// MY CODE
animator.SetInteger(-1); // Stay at current animation
...
// WITHIN UNITY (making it up)
Unity.UpdateStates(); // The number is read in the engine, nothing should happen
...
// MY CODE
animator.SetInteger(0); // The logic briefly changes to idle
...
// WITHIN UNITY (making it up)
Unity.UpdateStates(); // The number is read in the engine, transition to idle
...
// MY CODE
animator.SetInteger(4); // transition to north
...
// WITHIN UNITY (making it up)
// NOTHING HAPPENS!! It doesn't catch up
...
// MY CODE
animator.SetInteger(-1); // Stay at current state, assuming it changed to move_n
...
// WITHIN UNITY (making it up)
Unity.UpdateStates(); // The number is read in the engine, stays at idle_e!
...
UPDATE:
Here is the whole code:
void Update()
{
// update animator (Stupid transition from any state includes current state -_-")
int heading = CalculateHeading(horizontalInput, verticalInput);
if (heading != previousHeading)
{
anim.SetInteger("direction", heading);
previousHeading = heading;
}
else
{
anim.SetInteger("direction", -heading);
}
}
void MovementManagement ()
{
// If there is some axis input...
if(horizontalInput != 0f || verticalInput != 0f)
{
Vector3 position = transform.position;
position.x += horizontalInput * movementSpeed;
position.y += verticalInput * movementSpeed;
transform.position = position;
}
}
int CalculateHeading(float horizontal, float vertical)
{
if (horizontal == 0f && vertical == 0f)
{
return 0;
}
if (Mathf.Abs(horizontal) > Mathf.Abs(vertical))
{
return (horizontal > 0 ? 1 : 3);
}
else
{
return (vertical > 0 ? 4 : 2);
}
}
Update()
call? Maybe some of your real code would help to find the problem – Stefan Hoffmann