返回介绍

Animator.GetBehaviour 获得行为

发布于 2019-12-18 15:37:14 字数 4143 浏览 1341 评论 0 收藏 0

JavaScript => public function GetBehaviour(): T;
C# => public T GetBehaviour();

Description 描述

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

返回匹配类型T或派生自T的首个状态机行为,如果没有找到返回null。

JavaScript:

#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) {
		if (animator.GetComponent.<Recorder>().enabled && !animator.GetComponent.<Recorder>().isRecording)return ;
		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);
	}
}

C#:

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) 
	{
		if (animator.GetComponent<Recorder>().enabled && !animator.GetComponent<Recorder>().isRecording) return;
 
		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);
	}
}

Animator

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文