Search Unity

Need help locking movement for a character while attack animation,.using while function.

Discussion in '2D' started by RedRightHand-, Oct 10, 2021.

  1. RedRightHand-

    RedRightHand-

    Joined:
    Oct 10, 2021
    Posts:
    1
    Hello!

    I'm a Unity noob. I have successfully implemented animations for jumping, running and attacking to a character, but I want to disable movement while the attack animation is playing. This is the relevant code I used for the horizontal movement and attack. The solutions I've read haven't really helped me, but I have an idea to accomplish what I want.

    Code (CSharp):
    1. public float Speed;
    2.  
    3. private Rigidbody2D Rigidbody2D;
    4. private Animator Animator;
    5. private float Horizontal;
    6.  
    7. private void Start()
    8.     {
    9.         Rigidbody2D = GetComponent<Rigidbody2D>();
    10.         Animator = GetComponent<Animator>();
    11.     }
    12.  
    13. private void Update()
    14.     {
    15.         Horizontal = Input.GetAxisRaw("Horizontal");
    16.         if (Horizontal < 0.0f) transform.localScale = new Vector3(-1.0f, 1.0f, 1.0f);
    17.         else if (Horizontal > 0.0f) transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
    18.        
    19.         Animator.SetBool("Run", Horizontal != 0.0f);
    20.        
    21.        
    22.         if (Input.GetKeyDown(KeyCode.F))
    23.            {
    24.                Attack();
    25.            }
    26.        
    27.        //This is the "solution" I tried, but didn't work. It's an analog for .IsPlaying()            from the animation component.
    28.        while (Animator.GetCurrentAnimatorStateInfo(0).IsName("CharacterAttack") == true)
    29.           {
    30.               Speed = 0.0f;
    31.           }
    32.     }
    33.  
    34. private void Attack()
    35.     {
    36.         //The trigger for the animator that kick the attack animation.
    37.         Animator.SetTrigger("Attack");
    38.     }
    39.  
    40. private void FixedUpdate()
    41.     {
    42.         Rigidbody2D.velocity = new Vector2(Horizontal * Speed, Rigidbody2D.velocity.y);
    43.     }
    44.     }
    I had hoped that a "while" would work, but it doesn't. There's no error, but I would like to know how to implement this idea correctly.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    First, you can NEVER do a
    while()
    loop in Unity without making it a coroutine AND putting a
    yield return null;
    inside the loop; Unity is single-threaded from your perspective. See link below.

    Two general ways of locking out motion while some animation is in progress:

    Closed-loop way: use Animation Events or actively query the animation state to decide when the animation is done

    Open-loop way: lock out the behavior for an amount of time you pre-determine to match the animation (if you change the animation you must change the time).

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html