Wait For Animation Event node

nathanj

Active member
Here's a useful node script for waiting for an animation event.

You can change the string value if you have parallel functions running and are waiting for unique events.


Disclaimer: there might be an issue with manually connecting your game object to the node. I think that at the moment this will only work if the field is null. I'll have to research this further tomorrow.


Code:
using Opsive.UltimateCharacterController.Events;
using UnityEngine;

namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityAnimator
{
    [TaskCategory("Unity/Animator")]
    [TaskDescription("Waits for an Event from the Animator")]
    public class WaitForAnimationEvent : Action
    {

        [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
        public SharedGameObject targetGameObject;
        public string eventName = "OnContinue";
        private GameObject targetObject;
        private bool canContinue = false;

        public override void OnAwake()
        {
            targetObject = GetDefaultGameObject(targetGameObject.Value);
            EventHandler.RegisterEvent(targetObject, eventName, SendAnimEvent);
        }

        private void SendAnimEvent()
        {
            canContinue = true;
        }

        public override TaskStatus OnUpdate()
        {
           
            if (canContinue) {
                return TaskStatus.Success;
            }
            return TaskStatus.Running;
        }

        public override void OnReset()
        {
            targetGameObject = null;
            canContinue = false;
            EventHandler.UnregisterEvent(targetObject, eventName, SendAnimEvent);
        }
    }
}
 

Attachments

  • OnContinue.PNG
    OnContinue.PNG
    17.4 KB · Views: 7
Last edited:
Hey Justin

Im really sorry about this but do you mind having a look at this code whenever you get a chance? There's no rush, really

It worked when I only had one NPCwith a behaviour tree, but it seems that using it on multiple NPCs it no longer works.

Sorry about this,

Nathan
 
I can't see anything that would prevent it from working on multiple trees - as long as the event is sent to the GameObject with the event you should be good.
 
Thanks again for looking over it.

Once again I’m away from my computer but I think I might know what the problem is. At the moment I have all my NPCs using the same Animator Controller.

Say I have three NPCs using the same Animator Controller, if the above script is run will it only be sent to the specific NPC’s Animator Controller (the one with the behaviour tree that runs the script), or will it be sent to all NPCs with that Animator Controller?

I know I could just wait and test this Monday but I suspect this might be useful general knowledge in case others find themselves in my position.

Thank you,
Nathan
 
The same animator controller shouldn't be a problem - when Unity loads the animator controller it creates an instance of it so multiple animators can use the same controller and not interfere.
 
Top