Unity3D: When transition conditions are evaluated

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:

// MY CODE
animator.SetInteger(1); // Initially, it 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:

:

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);
    }
}
+4
1

. .

, . , update(). , , direction = -1 .

- atomic, , . false ... , .

, , IsInTransition(int layer) :

void Update()
{
    // update animator (Stupid transition from any state includes current state -_-")
    int heading = CalculateHeading(horizontalInput, verticalInput);

    if (heading != previousHeading && !anim.IsInTransition(0))  // you may need to adjust the layer index
    {
        anim.SetInteger("direction", heading);
        previousHeading = heading;
    }
    else
    {
        anim.SetInteger("direction", -heading);
    }
}

, , :) , , .

+2

Source: https://habr.com/ru/post/1537638/


All Articles