StateMachineBehaviour is a component that can be added to a state machine state. It's the base class every script on a state derives from.
By default the Animator does instantiate a new instance of each behaviour define in the controller. The class attribute SharedBetweenAnimatorsAttribute control how behaviours are instantiated.
StateMachineBehaviour has some predefined messages:
OnStateEnter, OnStateExit, OnStateIK, OnStateMove, OnStateUpdate.
#pragma strict public class AttackBehaviour extends StateMachineBehaviour { public var particle; public var radius; public var power; protected var clone; public override function OnStateEnter(animator, stateInfo, layerIndex) { clone = Instantiate(particle, animator.rootPosition, Quaternion.identity) as GameObject; var rb = clone.GetComponent.<Rigidbody>(); rb.AddExplosionForce(power, animator.rootPosition, radius, 3.0f); } public override function OnStateExit(animator, stateInfo, layerIndex) { Destroy(clone); } public override function OnStateUpdate(animator, stateInfo, layerIndex) { Debug.Log("On Attack Update "); } public override function OnStateMove(animator, stateInfo, layerIndex) { Debug.Log("On Attack Move "); } public override function OnStateIK(animator, stateInfo, layerIndex) { Debug.Log("On Attack IK "); } }
using UnityEngine;
public class AttackBehaviour : StateMachineBehaviour { public GameObject particle; public float radius; public float power; protected GameObject clone; override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { clone = Instantiate(particle, animator.rootPosition, Quaternion.identity) as GameObject; var rb = clone.GetComponent<Rigidbody>(); rb.AddExplosionForce(power, animator.rootPosition, radius, 3.0f); } override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { Destroy(clone); } override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { Debug.Log("On Attack Update "); } override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { Debug.Log("On Attack Move "); } override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { Debug.Log("On Attack IK "); } }
OnStateMachineEnter | Called on the first Update frame when a transition from a state from another statemachine transition to one of this statemachine's state. |
OnStateMachineExit | Called on the last Update frame when one of the statemachine's state is transitionning toward another state in another state machine. |
OnStateEnter | Called on the first Update frame when a statemachine evaluate this state. |
OnStateExit | Called on the last update frame when a statemachine evaluate this state. |
OnStateIK | Called right after MonoBehaviour.OnAnimatorIK. |
OnStateMove | Called right after MonoBehaviour.OnAnimatorMove. |
OnStateUpdate | Called at each Update frame except for the first and last frame. |
hideFlags | Should the object be hidden, saved with the scene or modifiable by the user? |
name | The name of the object. |
GetInstanceID | Returns the instance id of the object. |
ToString | Returns the name of the game object. |
Destroy | Removes a gameobject, component or asset. |
DestroyImmediate | Destroys the object obj immediately. You are strongly recommended to use Destroy instead. |
DontDestroyOnLoad | Makes the object target not be destroyed automatically when loading a new scene. |
FindObjectOfType | Returns the first active loaded object of Type type. |
FindObjectsOfType | Returns a list of all active loaded objects of Type type. |
Instantiate | Clones the object original and returns the clone. |
CreateInstance | Creates an instance of a scriptable object with className. |
bool | Does the object exist? |
operator != | Compares if two objects refer to a different object. |
operator == | Compares if two objects refer to the same. |