Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

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

Animator.GetBehaviour

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

Switch to Manual
public method GetBehaviour(): T;
public T GetBehaviour();

Description

Return the first StateMachineBehaviour that match type T or derived from T. Return null if none are found.

#pragma strict
public class RunBehaviour extends StateMachineBehaviour {
	// OnStateUpdate is called at each Update frame between OnStateEnter and OnStateExit callback
	public override function OnStateUpdate(animator: Animator, stateInfo: AnimatorStateInfo, layerIndex: int) {
		var transform: Transform = animator.GetComponent.<Transform>();
		var hitInfo: RaycastHit;
		var dir: Vector3 = transform.TransformDirection(Vector3.forward);
		if (Physics.Raycast(transform.position + new Vector3(0, 1.5f, 0), dir, hitInfo, 10)) {
			if (hitInfo.collider.tag == "Obstacle") {
				animator.GetBehaviour.<SlideBehaviour>().target = transform.position + 1.25f * hitInfo.distance * dir;
				if (hitInfo.distance < 6)
					animator.SetTrigger("Slide");
			}
		}
	}
}
public class SlideBehaviour extends StateMachineBehaviour {
	public var target: Vector3;
	public var slideMatchTargetStart: float = 0.11f;
	public var slideMatchTargetStop: float = 0.40f;
	// OnStateUpdate is called at each Update frame between OnStateEnter and OnStateExit callback
	public override function OnStateUpdate(animator: Animator, stateInfo: AnimatorStateInfo, layerIndex: int) {
		animator.MatchTarget(target, new Quaternion(), AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(1, 0, 1), 0), slideMatchTargetStart, slideMatchTargetStop);
	}
}
using UnityEditor;
using UnityEngine;

public class RunBehaviour : StateMachineBehaviour { // OnStateUpdate is called at each Update frame between OnStateEnter and OnStateExit callback override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { Transform transform = animator.GetComponent<Transform>();

RaycastHit hitInfo; Vector3 dir = transform.TransformDirection(Vector3.forward); if (Physics.Raycast(transform.position + new Vector3(0, 1.5f, 0), dir, out hitInfo, 10)) { if (hitInfo.collider.tag == "Obstacle") { animator.GetBehaviour<SlideBehaviour>().target = transform.position + 1.25f * hitInfo.distance * dir; if (hitInfo.distance < 6) animator.SetTrigger("Slide"); } } } }

public class SlideBehaviour : StateMachineBehaviour { public Vector3 target;

public float slideMatchTargetStart = 0.11f; public float slideMatchTargetStop = 0.40f;

// OnStateUpdate is called at each Update frame between OnStateEnter and OnStateExit callback override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { animator.MatchTarget(target, new Quaternion(), AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(1, 0, 1), 0), slideMatchTargetStart, slideMatchTargetStop); } }

Did you find this page useful? Please give it a rating: