[Unity Animation] Call the function when the animation plays to a certain frame - add events Events

No matter which frame the animation plays, we can "ambush" an event here (call a function and pass a parameter to the function, the parameter is set externally, or even an object is passed)!

Hi, dear Unity friends! Have you ever wanted to add some special moments to your animations to make them more alive? Then, Animation Events is your magic weapon! This article will give you an in-depth understanding of all aspects of Unity animation events.

1. Introduction to Events of animation clips

Before talking about Unity animation events, let's first understand how it works. Animation events allow you to execute functions in scripts on specified keyframes during animation playback. This means you can trigger custom logic at different moments in the animation to breathe more life into characters, scenes, or other elements.

2. Examples of practical application scenarios

2.1 Sound effect trigger

Imagine that the moment your game character swings a weapon, can a cool sound effect be played at this moment? With Animation Events, you can easily achieve this.

Then create a script,The script must be hung on the game object that plays the animation in the scene!

The function name must be the same as the name of the external Function

like:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventaddFrame : MonoBehaviour
{
    public void PlaySoundEffect(int para)
    {

        Debug.Log("播放到第15帧了,开始播放声音" + para);
    }

}

2.2 Character attack determination

Maybe you want to perform an attack judgment on a certain frame of the character's attack action to detect whether it hits the enemy. Yes, through Animation Events, you can trigger decision logic at this critical moment.

public void AttackHitCheck() { // 在这里添加攻击判定的逻辑 }

3. Specific adding method

3.1 Open the animation window

First, in the Unity editor, open the animation window. You can open it by double-clicking the imported model file or selecting the model and clicking the "Open Animation Window" button.

3.2 Select animation clips

In the animation window, select the specific animation clip for which you want to add events.

3.3 Add events

Right-click the frame and select "Add Event". In the event view, set the trigger time of the event, the calling function name and possible parameters.

4. Precautions

To make sure everything runs smoothly, please note the following:

  • Function access: Make sure your script functions are public so that the Animator system can access them.

  • When the event fires: Carefully choose when the event fires, making sure it's on the correct keyframe.

  • Function name: must be the same as the function name

  • Object parameter: It is to pass a game object to the function. The function needs a variable to receive it.

  • The script must be hung on: the game object that plays the animation

5. Sample code

Here is a simple example code showing how to define a function in a script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventaddFrame : MonoBehaviour
{
    public void PlaySoundEffect(int para)
    {

        Debug.Log("播放到第15帧了,开始播放声音" + para);
    }
    public void AttackHitCheck(float para)

    { // 在这里添加攻击判定逻辑 

        Debug.Log("播放到第25帧了,开始攻击" + para);
    }
}

6. Case

6.1 The sword shines brightly

In the animation of the character wielding the weapon, the flash of the sword light special effect is triggered through Animation Events, which adds a lot of color to the battle scene.

6.2 Dialogue system trigger

In the animation of character expression changes, the dialogue system is triggered through Animation Events, making the character animation more interactive.

6.3 Play sound effects

Supongo que te gusta

Origin blog.csdn.net/leoysq/article/details/134762855
Recomendado
Clasificación