Search Unity

Animation gets stuck at the end in Unity Animator

Discussion in 'Animation' started by markovugerlol, Feb 18, 2021.

  1. markovugerlol

    markovugerlol

    Joined:
    Jun 15, 2020
    Posts:
    1
    I am not good at animating in 3D so I have a slight problem. I am making a swinging animation with a bat. Every animation works perfectly fine, but what's the problem here is that when from idle animation goes to swing animation it gets stuck, I have to click few times to return to idle state.


    This is how animation looks like. Here is where it gets stuck:

    At the end of "swing2" animation it stops, and I have to click 2 or 3 times to get through transition back to "idlestate". Here I am showing you the inspector of these two transitions:


    I am trying to solve this problem for some time now and still don't get it why it's happening.
    Here you can check out the code for this. This script is specifically for melee weapons but I think it can apply to other items as well. Everything in the code works except this animator part.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class melee : MonoBehaviour
    4. {
    5.     public float dmg;
    6.     public float range;
    7.  
    8.     private Animator anim;
    9.     private bool attacking = false;
    10.  
    11.     public Camera cam;
    12.  
    13.     void Start(){
    14.         anim = gameObject.GetComponent<Animator>();
    15.     }
    16.  
    17.  
    18.  
    19.     void FixedUpdate(){
    20.         AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(0);
    21.         if(Input.GetButtonDown("Fire1")){
    22.             Attack();
    23.          
    24.         }else if(Input.GetButtonUp("Fire1")){
    25.             anim.SetBool("attacking", false);
    26.         }
    27.  
    28.     }
    29.  
    30.     void Attack(){
    31.         RaycastHit hit;
    32.         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range)){
    33.             Debug.Log(hit.transform.name);
    34.  
    35.             enemyHealth enemy = hit.transform.GetComponent<enemyHealth>();
    36.          
    37.             if(enemy != null){
    38.                 enemy.TakeDmg(dmg);
    39.             }
    40.  
    41.         }
    42.  
    43.  
    44.         anim.SetBool("attacking", true);
    45.     }
    46. }
    47.  
    Also how do you add a blocker so that you cant swing or attack again until animation finishes?
     
  2. M-Elwy

    M-Elwy

    Joined:
    Jan 28, 2021
    Posts:
    38
    To avoid input loss, I would move checking user input to
    Update
    as it runs every frame, instead of
    FixedUpdate
    which doesn't.
    And i guess there is no need to check for
    GetButtonUp
    in
    else
    statement.