unity-game-engineanimator

Changing an object's position in Update() prevents the animation from playing


I'm making a turn-based grid-based game with a top-down perspective. Enemies move when the player moves, like a traditional roguelike.

Each character in the game (including the hero and enemies) has a futurePosition attribute that determines where they're supposed to move next. When the player presses a direction key, the hero's futurePosition is set to the direction indicated by the key, then every enemies' futurePosition is updated as well. The code in the Update() function of those characters looks like this :

public virtual void Update() {
    if (MustMoveObject()) {
        transform.position = Vector3.MoveTowards(transform.position, futurePosition, inverseMoveTime * Time.deltaTime);
    } else if (attacking) {
        Attack();
        futurePosition = Vector3Int.RoundToInt(futurePosition);
    }
}

Note: MustMoveObject() returns true if futurePosition and transform.position are not equal

If the game detects there's an enemy in the direction the player is going towards, it will attack it instead. The code looks like this:

MovingObject enemyToAttack = GameManager.instance.CheckForCreatureCurrentPosition(pos);
if (enemyToAttack != null)
{
    target = enemyToAttack;
    cameraFollow=false;
    attacking=true;
    animator.SetTrigger("attack");
    futurePosition = Vector3.Lerp(transform.position, pos, 0.2f);
} else {
    target=null;
    cameraFollow=true;
    futurePosition = pos;
}

As you can see in the code above, when attacking an enemy, the futurePosition attribute is set at about one fifth (or 20%) of the distance between the hero and the enemy, and "attacking" is set to true. When the player reaches that point in the Update() function, it attacks the enemy and then goes back to its original position. I hope this is clear so far.

Here's my problem : I recently added a test animation for the hero when it attacks, but the animation doesn't start when it should at animator.SetTrigger("attack");. It only activates after the movements of both the hero and the enemies is over. I know that this is the problem because if I replace the line transform.position = Vector3.MoveTowards(transform.position, futurePosition, inverseMoveTime * Time.deltaTime); with transform.position = futurePosition;, it makes the deplacement immediate and the animation triggers instantly. I want the animation to occur while the object is moving towards futurePosition.

Here's what the transition looks like in the inspector (I start from AnyState instead of IdleRight because I just wanted to test things out) :

Screenshot

As you can see, there's no exit time. There's also no delay in the animation itself, I've checked. Everything else is working as expected.

I'm also using Unity version 2019.4.37f1, I'm not sure whether that's relevant.

I hope I made my problem clear and that someone has an idea of what to do. I've searched for a solution, but it didn't seem like anyone had the same issue. If anything, it seemed most people had the opposite problem, where the object wouldn't move as long as the animation was active... Thanks in advance for your help.


Solution

  • Well, I figured it out... It was very dumb.

    I needed to check "Has Exit Time" on for the transition between "Any State" and the various Idle States in the inspector. That was literally just it. :/