I have a very simple game in Unity, and unfortunately it does not work as expected.
Context
I have the following setup:
http://mazyod.com/images/Screenshot_2014-04-23_10.51.20.png
From any state, if the integer parameter directionis equal 1, set the animation player_move_e. The same applies to 2, 3, 4for other destinations. If directionset to 0, idle animation associated with direction is played.
It’s important to note that because I use the “Any State” block, I have to make sure that as soon as the animation state changes to one of the animation states of the movement, I reset directionto the sentinel value, for example -1, which stores the animation state in the current state and does not change state for itself again.
Problem
When a player is in a state of, say, move_nand I quickly change direction to, say, move_e(not giving him enough time to go idle), the animation state is stuck at move_n: (
How can I find out if a transition has occurred and I am not updating too often? I originally updated directionto FixedUpdate, and then moved it to Update, but this is the same problem in both methods.
Here is the logic I see:
animator.SetInteger(1);
...
Unity.UpdateStates();
...
animator.SetInteger(-1);
...
Unity.UpdateStates();
...
animator.SetInteger(0);
...
Unity.UpdateStates();
...
animator.SetInteger(4);
...
...
animator.SetInteger(-1);
...
Unity.UpdateStates();
...
UPDATE:
:
void Update()
{
int heading = CalculateHeading(horizontalInput, verticalInput);
if (heading != previousHeading)
{
anim.SetInteger("direction", heading);
previousHeading = heading;
}
else
{
anim.SetInteger("direction", -heading);
}
}
void MovementManagement ()
{
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);
}
}