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

Animation StateMachineBehaviour executing too early

Discussion in 'Scripting' started by MaximumTre, Feb 2, 2020.

  1. MaximumTre

    MaximumTre

    Joined:
    Nov 28, 2015
    Posts:
    163
    Hello.

    I am using a StateMachineBehaviour in the Animator to control when a hitbox is activated for player attacks. When this animation is used rapidly, the hitbox is activated too early. Here is the State machine code:

    Code (CSharp):
    1. public class MeleeAttack : StateMachineBehaviour
    2. {
    3.     public bool attackActive = false, finished = false;
    4.     public float normalizedStartTime = 0, endTime, thrustForce;
    5.     public AttackInfo Info;
    6.  
    7.     // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    8.     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    9.     {
    10.         attackActive = false;
    11.         finished = false;
    12.         Debug.Log("Animation Normalized Time" + stateInfo.normalizedTime.ToString());
    13.     }
    14.  
    15.     // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    16.     override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    17.     {
    18.         if(stateInfo.normalizedTime >= normalizedStartTime)
    19.         {        
    20.             if (!attackActive)
    21.             {
    22.                 // Activate hit boxes
    23.                 attackActive = true;
    24.                 animator.gameObject.transform.SendMessage("EmitAttack", Info);
    25.                 //animator.gameObject.transform.parent.GetComponent<Rigidbody2D>().AddForce(animator.gameObject.transform.forward * thrustForce, ForceMode2D.Impulse);
    26.             }          
    27.         }
    28.  
    29.         if (stateInfo.normalizedTime >= endTime)
    30.         {
    31.             if(!finished)
    32.             {
    33.                 // Reset Melee Attack
    34.                 animator.gameObject.transform.SendMessage("SetAttackOK");
    35.                 finished = true;
    36.             }
    37.         }
    38.     }
    39.  
    40.     // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    41.     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    42.     {
    43.      
    44.     }
    45.  
    46.     // OnStateMove is called right after Animator.OnAnimatorMove()
    47.     //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    48.     //{
    49.     //    // Implement code that processes and affects root motion
    50.     //}
    51.  
    52.     // OnStateIK is called right after Animator.OnAnimatorIK()
    53.     //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    54.     //{
    55.     //    // Implement code that sets up animation IK (inverse kinematics)
    56.     //}
    57. }
    When EmitAttack is called, it sets a bool to false which prevents further attacks until SetAttackOK is called, so this should prevent this but it doesn't. Any one encounter this before and know how to stop it from happening? It looks weird when the damage starts to pop up before the attack is even finished.
     
    pepperopigeon likes this.
  2. MaximumTre

    MaximumTre

    Joined:
    Nov 28, 2015
    Posts:
    163
    Ye olde bump
     
  3. MaximumTre

    MaximumTre

    Joined:
    Nov 28, 2015
    Posts:
    163


    Here is a video that will show what the problem is. Whenever an attack is repeated quickly, the damage frame comes out sooner than expected. I hope someone can help with this.
     
  4. CYCLE-6

    CYCLE-6

    Joined:
    Jul 26, 2017
    Posts:
    7
    Got the exact same problem. I'm guessing the OnStateEnter/OnStateExit events are just not a good way of doing such a task. Seems I need to find a better way.