版本: Unity 6 (6000.0)
语言英语
  • C#

Animator.GetBehaviour

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交的内容,但我们会阅读用户提出的每项更改建议,并在适用情况下进行更新。

关闭

提交失败

由于某些原因,您的更改建议无法提交。请在几分钟后<a>重试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

切换到手册

声明

public T GetBehaviour();

描述

返回与类型 T 匹配或从 T 派生的第一个 StateMachineBehaviour。如果未找到,则返回 null。

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); } }