Search Unity

Animation interrupted by another animation; how to fix?

Discussion in 'Scripting' started by silverleecams, Jul 14, 2022.

  1. silverleecams

    silverleecams

    Joined:
    May 12, 2022
    Posts:
    51
    This script connects to the Animator and Character Controller components to make the character walk, attack, etc.

    Works like a charm, however if I try to click while moving, the Attack animation is short-stopped by the Walk animation. The character ends up jittering instead of animating if a moving attack is attempted.

    - Is there a way to check if an animation is still playing within the code?
    - How to make the script to allow Attack to finish before Walk? I have tried tinkering with Exit Time to no avail.
     
  2. silverleecams

    silverleecams

    Joined:
    May 12, 2022
    Posts:
    51
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     public GameObject player;
    4.  
    5.     // Update is called once per frame
    6.     void Update()
    7.     {
    8.         if (Input.GetButton("Vertical"))
    9.         {
    10.             player.GetComponent<Animator>().Play("Walk");
    11.             Debug.Log("player moving");
    12.         }
    13.         if (Input.GetButton("Horizontal"))
    14.         {
    15.             player.GetComponent<Animator>().Play("Walk");
    16.             Debug.Log("player moving");
    17.         }
    18.         if (Input.GetButtonDown("Fire1"))
    19.         {
    20.             player.GetComponent<Animator>().Play("Attack");
    21.         }
    22.     }
    23. }
    24.  
     
  3. silverleecams

    silverleecams

    Joined:
    May 12, 2022
    Posts:
    51
    Update: .Stop() is deprecated I guess. This is gonna suck. Any ideas??
     
  4. silverleecams

    silverleecams

    Joined:
    May 12, 2022
    Posts:
    51
    Update: Changed to "GetButtonDown". Problem solved.

    edit: except the characters stop animating. Ugh!
     
    Last edited: Jul 14, 2022
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Are you keeping the Animator controller window visible and the object selected? You can learn a lot about how things are misbehaving via that visual technique.
     
    silverleecams likes this.
  6. silverleecams

    silverleecams

    Joined:
    May 12, 2022
    Posts:
    51
    Yes and no answers.