• Editor
  • How to trigger LipSync animation (+ audio) on click

I exported a Spine project containing two animations, one looped, the base animation. And the second which is the lip sync animation with audio.

I would like to understand how to do this in Unity, so that the basic animation always plays in a non-stop loop, and when the player clicks on the creature, it triggers the lip sync animation + audio once and then the basic animation restarts in a loop.

I imported using "Skeleton Animation"

Related Discussions
...

To achieve the desired behavior in Unity using Spine, you can follow these steps:

  1. Set Up Your Animations: Ensure you have two animations in your Spine project: one for the base loop animation and another for the lip sync animation. Export these animations and import them into Unity.

  2. Use SkeletonAnimation Component: Attach the SkeletonAnimation component to your GameObject in Unity. This component will allow you to control the animations.

  3. Play Base Animation in a Loop: Use the SetAnimation method to play the base animation in a loop. This can be done in the Start method of your script:

       skeletonAnimation.AnimationState.SetAnimation(0, "baseAnimation", true);
  4. Trigger Lip Sync Animation on Click: Add a method to handle the click event. In this method, use SetAnimation to play the lip sync animation on a higher track (e.g., track 1) so it overrides the base animation temporarily. Set the loop parameter to false for the lip sync animation:

       void OnMouseDown() {
           skeletonAnimation.AnimationState.SetAnimation(1, "lipSyncAnimation", false);
           // Optionally, play audio here
       }
  5. Return to Base Animation: After the lip sync animation finishes, the base animation will automatically continue playing because it is set on track 0 and is looped.

  6. Play Audio: If you want to play audio along with the lip sync animation, you can trigger an audio source to play the sound when the lip sync animation starts.

Here's a simple example script:

using UnityEngine;
using Spine.Unity;

public class LipSyncController : MonoBehaviour {
    public SkeletonAnimation skeletonAnimation;
    public AudioSource audioSource;

    void Start() {
        skeletonAnimation.AnimationState.SetAnimation(0, "baseAnimation", true);
    }

    void OnMouseDown() {
        skeletonAnimation.AnimationState.SetAnimation(1, "lipSyncAnimation", false);
        if (audioSource != null) {
            audioSource.Play();
        }
    }
}

This script assumes you have an AudioSource component attached to the same GameObject or assigned in the inspector.