• Unity
  • Newbie Question regarding events

Related Discussions
...

I have events (actually, event singular) set up in my animation. I have coded in all the the listener functions and it works well 95% of the time. It is that other 5% that is the problem. Occasionally, it seems that the event is heard. Any guesses as to what would cause this?

Here is the code I'm using:

[SpineEvent(dataField: "skeletonAnimation", fallbackToTextField: true)]
    public string eventName = "TurnComplete";

public Spine.Unity.SkeletonAnimation skeletonAnimation;
public JellyDirectionSetter directionSetter;

public Spine.Unity.AnimationReferenceAsset stillRight;
public Spine.Unity.AnimationReferenceAsset stillLeft;
public Spine.Unity.AnimationReferenceAsset danceRight;
public Spine.Unity.AnimationReferenceAsset danceLeft;
public Spine.Unity.AnimationReferenceAsset leftToRight;
public Spine.Unity.AnimationReferenceAsset rightToLeft;

public bool animPlaying = false;

public JellyMoveDirection moveDirection = JellyMoveDirection.right;

Spine.EventData eventData;

public LayerMask clickMask;

// Start is called before the first frame update
void Start()
{

    eventData = skeletonAnimation.Skeleton.Data.FindEvent(eventName);
    skeletonAnimation.AnimationState.Event += HandleAnimationStateEvent;


    directionSetter.moveDirection = moveDirection;

    if(moveDirection == JellyMoveDirection.right)
    {
        skeletonAnimation.AnimationState.SetAnimation(0, stillRight, false);
    }
    else
    {
        skeletonAnimation.AnimationState.SetAnimation(0, stillLeft, false);
    }

}

private void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        RaycastHit2D[] hit2D = Physics2D.GetRayIntersectionAll(ray, Mathf.Infinity, clickMask.value);

        if (hit2D.Length > 0)
        {
            foreach (RaycastHit2D hit in hit2D)
            {
                Debug.Log(hit.collider.gameObject.name);
                if(hit.collider.gameObject == gameObject)
                {
                    Clicked();
                    return;
                }

            }
        }
    }
}


private void HandleAnimationStateEvent(Spine.TrackEntry trackEntry, Spine.Event e)
{
    Debug.Log(e.Data.Name);
    //bool eventMatch = string.Equals(e.Data.Name, eventName, System.StringComparison.Ordinal); // Testing recommendation: String compare.
    bool eventMatch = (eventData == e.Data); // Performance recommendation: Match cached reference instead of string.
    if (eventMatch)
    {
        animPlaying = false;
    }
}

There's more, but this is the relevant part. The problem is that, if that event isn't heard, the object in question remains unclickable. Any advice is appreciated.

It's worth noting that this is in play mode in the editor. I don't know if that makes any difference at all.

Your code looks good so far, I can see nothing that would explain incorrect event behaviour. Maybe there is some kind of border-problem of the event being at or after the last frame. If it is placed at the end, could you try moving the event to e.g. the middle of the animation duration and tell me if this fixes it?

I changed the mix duration and that seems to have fixed the issue. I wonder if the event was possibly getting missed if it happened during a transition.

That is indeed the likely cause. When you queue an animation via AnimationState, you get a TrackEntry. That has a property EventThreshold, which governs if events from that animation you queued are being fired during mixing or not. It defaults to 0, so no events are being fired for that animation during mix-out.

Thanks @badlogic for pointing this out. You could set the EventThreshold parameter of your track entries to 1.0 (= end of transition) if you want to receive events even during the transition to other animations.