• Animator.GetBehaviour 获得行为

    Animator.GetBehaviour 获得行为

    public T GetBehaviour();

    描述:

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

    1. public class RunBehaviour : StateMachineBehaviour {
    2. // OnStateUpdate is called at each Update frame between OnStateEnter and OnStateExit callback
    3. override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    4. {
    5. if (animator.GetComponent<Recorder>().enabled && !animator.GetComponent<Recorder>().isRecording) return;
    6. Transform transform = animator.GetComponent<Transform>();
    7. RaycastHit hitInfo;
    8. Vector3 dir = transform.TransformDirection(Vector3.forward);
    9. if (Physics.Raycast(transform.position + new Vector3(0, 1.5f, 0), dir, out hitInfo, 10))
    10. {
    11. if (hitInfo.collider.tag == "Obstacle")
    12. {
    13. animator.GetBehaviour<SlideBehaviour>().target = transform.position + 1.25f * hitInfo.distance * dir;
    14. if(hitInfo.distance < 6)
    15. animator.SetTrigger("Slide");
    16. }
    17. }
    18. }
    19. }
    20. public class SlideBehaviour : StateMachineBehaviour {
    21. public Vector3 target;
    22. public float slideMatchTargetStart = 0.11f;
    23. public float slideMatchTargetStop = 0.40f;
    24. // OnStateUpdate is called at each Update frame between OnStateEnter and OnStateExit callback
    25. override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    26. animator.MatchTarget(target, new Quaternion(), AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(1, 0, 1), 0), slideMatchTargetStart, slideMatchTargetStop);
    27. }
    28. }