Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

StateMachineBehaviour

class in UnityEngine

/

Inherits from:ScriptableObject

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

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: GameObject;
	public var radius: float;
	public var power: float;
	protected var clone: GameObject;
	public override function OnStateEnter(animator: Animator, stateInfo: AnimatorStateInfo, layerIndex: int) {
		clone = Instantiate(particle, animator.rootPosition, Quaternion.identity) as GameObject;
		var rb: Rigidbody = clone.GetComponent.<Rigidbody>();
		rb.AddExplosionForce(power, animator.rootPosition, radius, 3.0f);
	}
	public override function OnStateExit(animator: Animator, stateInfo: AnimatorStateInfo, layerIndex: int) {
		Destroy(clone);
	}
	public override function OnStateUpdate(animator: Animator, stateInfo: AnimatorStateInfo, layerIndex: int) {
		Debug.Log("On Attack Update ");
	}
	public override function OnStateMove(animator: Animator, stateInfo: AnimatorStateInfo, layerIndex: int) {
		Debug.Log("On Attack Move ");
	}
	public override function OnStateIK(animator: Animator, stateInfo: AnimatorStateInfo, layerIndex: int) {
		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; Rigidbody 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 "); } }

Public Functions

OnStateMachineEnterCalled on the first Update frame when making a transition to a StateMachine. This is not called when making a transition into a StateMachine sub-state.
OnStateMachineExitCalled on the last Update frame when making a transition out of a StateMachine. This is not called when making a transition into a StateMachine sub-state.

Messages

OnStateEnterCalled on the first Update frame when a statemachine evaluate this state.
OnStateExitCalled on the last update frame when a statemachine evaluate this state.
OnStateIKCalled right after MonoBehaviour.OnAnimatorIK.
OnStateMoveCalled right after MonoBehaviour.OnAnimatorMove.
OnStateUpdateCalled at each Update frame except for the first and last frame.

Inherited members

Variables

hideFlagsShould the object be hidden, saved with the scene or modifiable by the user?
nameThe name of the object.

Public Functions

GetInstanceIDReturns the instance id of the object.
ToStringReturns the name of the game object.

Static Functions

DestroyRemoves a gameobject, component or asset.
DestroyImmediateDestroys the object obj immediately.
DontDestroyOnLoadMakes the object target not be destroyed automatically when loading a new scene.
FindObjectOfTypeReturns the first active loaded object of Type type.
FindObjectsOfTypeReturns a list of all active loaded objects of Type type.
InstantiateClones the object original and returns the clone.
CreateInstanceCreates an instance of a scriptable object.

Operators

boolDoes the object exist?
operator !=Compares if two objects refer to a different object.
operator ==Compares two object references to see if they refer to the same object.

Messages

AwakeThis function is called when the ScriptableObject script is started.
OnDestroyThis function is called when the scriptable object will be destroyed.
OnDisableThis function is called when the scriptable object goes out of scope.
OnEnableThis function is called when the object is loaded.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961