Search Unity

Question How can Animation Event call a function when we are in State_Behaviour

Discussion in 'Animation' started by unity_408540137, Feb 22, 2023.

  1. unity_408540137

    unity_408540137

    Joined:
    Oct 23, 2022
    Posts:
    44
    Usually, We have one player attached with method and animation (ex:attack)
    Then we select the animation frame for right time and add Event, and call function which we had created before.

    But when we use State_Behaviour, I don't know how to do the mentioned above.

    State_Behaviours seems not suitable.for using animation event calling a function
     
  2. donotprikel

    donotprikel

    Joined:
    Nov 14, 2022
    Posts:
    26
    In Unity an Animation Event can call a function when we are in a State Behaviour by using the OnStateIK or OnStateUpdate methods.

    The OnStateIK method is called every frame after the Animator.Update() method is called, and it is typically used for adjusting the position and rotation of an IK target. To use an Animation Event to call a function when in this state, you can simply add an Animation Event to your animation timeline and set its function name to the name of the method you want to call.

    The OnStateUpdate method is called every frame after Animator.Update() and is typically used for updating the state machine. To use an Animation Event to call a function when in this state, you will need to create a custom Script that implements the StateMachineBehaviour class. This script can then be attached to the state in question. Once attached, you can then add an Animation Event to your animation timeline and set its function name to the name of the method you want to call, which will be defined within your custom Script.


    Heres a little script as an example that will do what you asked for. :)

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CustomStateBehaviour : StateMachineBehaviour
    4. {
    5.     public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    6.     {
    7.         // Your code here for updating the state machine
    8.  
    9.         // Check for Animation Event with the function name "MyFunction"
    10.         if (stateInfo.IsName("StateName") && stateInfo.normalizedTime > 0.5f)
    11.         {
    12.             animator.gameObject.SendMessage("MyFunction");
    13.         }
    14.     }
    15. }
    16.  
     
  3. unity_408540137

    unity_408540137

    Joined:
    Oct 23, 2022
    Posts:
    44
    thanks, I will try.
    I try to make state_beaviour for emeries in Game.
    For examples, the emeries randomly walk and automatically attack or play magic, for a 3D fight Game
     
  4. NovaesDev

    NovaesDev

    Joined:
    Jul 22, 2018
    Posts:
    3
    It's not the best way to simulate the AnimationEvent, but it's functional.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NPC : StateMachineBehaviour
    6. {
    7.     private bool m_OnePlay = true;
    8.     private float m_Normalized;
    9.  
    10.     //if the animation is looped in, normalized is not reset
    11.     private int m_NormalizedContinuous;
    12.     //if the animation is looped in, normalized is not reset
    13.     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    14.     {
    15.         ResetPlay();
    16.     }
    17.  
    18.     override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    19.     {
    20.         m_Normalized = stateInfo.normalizedTime;
    21.  
    22.         if (m_Normalized >= m_NormalizedContinuous && m_OnePlay)
    23.         {
    24.             m_OnePlay = false;
    25.             m_NormalizedContinuous++;
    26.             animator.gameObject.SendMessage("MyFunction");
    27.         }
    28.  
    29.         //when it gets halfway through the animation it will restart oneplay
    30.         if (m_Normalized >= (m_NormalizedContinuous - 0.5f) && !m_OnePlay) ResetPlay();
    31.     }
    32.  
    33.     public void ResetPlay()
    34.     {
    35.         m_OnePlay = true;
    36.         if (m_Normalized <= 1) m_NormalizedContinuous = 1;
    37.     }
    38. }
    39.  
     
  5. donotprikel

    donotprikel

    Joined:
    Nov 14, 2022
    Posts:
    26
    I know i was verry bad at script back then but now a little better because i dont use animator anymore i use legacy animations