Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Animation' started by Apoz, Oct 24, 2022.

  1. Apoz


    Apr 13, 2022
    I'm making 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 :

    Code (CSharp):
    1. public virtual void Update() {
    2.     if (MustMoveObject()) {
    3.         transform.position = Vector3.MoveTowards(transform.position, futurePosition, inverseMoveTime * Time.deltaTime);
    4.     } else if (attacking) {
    5.         Attack();
    6.         futurePosition = Vector3Int.RoundToInt(futurePosition);
    7.     }
    8. }
    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:

    Code (CSharp):
    1. MovingObject enemyToAttack = GameManager.instance.CheckForCreatureCurrentPosition(pos);
    2. if (enemyToAttack != null)
    3. {
    4.     target = enemyToAttack;
    5.     cameraFollow=false;
    6.     attacking=true;
    7.     animator.SetTrigger("attack");
    8.     futurePosition = Vector3.Lerp(transform.position, pos, 0.2f);
    9. } else {
    10.     target=null;
    11.     cameraFollow=true;
    12.     futurePosition = pos;
    13. }
    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
    Code (CSharp):
    1. transform.position = Vector3.MoveTowards(transform.position, futurePosition, inverseMoveTime * Time.deltaTime);
    Code (CSharp):
    1. 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) :

    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.
    Last edited: Oct 24, 2022
  2. Apoz


    Apr 13, 2022
    Well, I figured it out... It was very dumb. I'll just post the solution in case someone has the same problem.

    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. :/