- Home /
State Machine Behaviour together with Animation Events
Hello!
I've recently decided to make use of the SMB's, but I stumbled upon some issues (obviously) :-) .
I was hoping to create a state machine behaviour script with some methods, which could be invoked by the animation event feature (those events we define inside an animation clip). Unfortunately, the event list is empty, even though the SMB script is attached to the same state as the animation clip.
Is it in any way possible to reference a method from SMB in an animation clip? Or are we limited to a method on a monobehaviour which is on the same object as the animator?
The whole point of it was to invoke the method on SMB from the clip, and then broadcast it from SMB as a c# event, like so:
// this method should be invoked by an animation event
public void OnSomethingHappened()
{
if (SomethingHappened != null)
SomethingHappened();
}
And this brings me to another issue.. According to the documentation, there are only two ways to get the reference to SMB's:
animator.GetBehaviour< T>()
and
animator.GetBehaviours< T>()
I was hoping to get the script from a concrete state (like transform.find("xxx").getComponent() ), but I could not find a corresponding method. That would allow me to reuse the same script on a few states ("Attack_Basic", "Attack_Advanced" and such).
Are these things possible in current version of Unity, or maybe I'm simply trying to utilize SMB's not the way they were supposed to be used? :-)
Answer by L42yB · Jul 18, 2018 at 08:32 PM
For your first question, an animation event will only call a function on a script that inherits MonoBehaviour - so it won't work on a SMB script.
For the second, you can get the behaviour off an instanced game object like this:
gameObject.GetComponent<Animator>().GetBehaviours<BehaviourName>();
Your answer
Follow this Question
Related Questions
Animation Plays well,but teleportation effect happens briefly when parenting the object to other obj 0 Answers
StateMachineBehaviour.OnDestroy is getting called by SetActive 1 Answer
"Anim A -> Anim B, Anim C -> Anim B" or "Any State -> Anim B" would there be a difference? 0 Answers
AnimationEvent has no receiver, but there is no AnimationEvent 1 Answer
Animator keeps overriding properties, though the current animation does not touch them 0 Answers