官方 Animator 例子解析 Animator.MatchTarget
一、官方的解释
1 Animator.MatchTargetSwitch to Manual 2 void MatchTarget(Vector3 matchPosition, Quaternion matchRotation, AvatarTarget targetBodyPart, MatchTargetWeightMask weightMask, float startNormalizedTime, float targetNormalizedTime = 1); 3 Parameters 4 5 matchPosition The position we want the body part to reach. 6 matchRotation The rotation in which we want the body part to be. 7 targetBodyPart The body part that is involved in the match. 8 weightMask Structure that contains weights for matching position and rotation. 9 startNormalizedTime Start time within the animation clip (0 - beginning of clip, 1 - end of clip). 10 targetNormalizedTime End time within the animation clip (0 - beginning of clip, 1 - end of clip), values greater than 1 can be set to trigger a match after a certain number of loops. Ex: 2.3 means at 30% of 2nd loop. 11 Description 12 13 Automatically adjust the gameobject position and rotation so that the AvatarTarget reaches the matchPosition when the current state is at the specified progress. 14 15 Target matching only works on the base layer (index 0).
二、理解
这个 一般 用于 根运动 动画。 可以用在 人物攀爬 或者 掉落 的动画 时候 进行一些 位置的调整。具体在于理解。上一段官方的例子教程。
1 using UnityEngine; 2 using System.Collections; 3 4 public class TargetMatching : MonoBehaviour 5 { 6 7 private Animator animator; 8 /// <summary> 9 /// 匹配 目标 10 /// </summary> 11 public Transform RightHand; 12 bool hasJumped = false; 13 14 15 // Use this for initialization 16 void Start () { 17 18 animator = GetComponent<Animator>(); 19 20 } 21 22 void OnGUI() 23 { 24 GUILayout.Label("Make your character grab any edge!"); 25 GUILayout.Label("Press Fire1 to trigger Jump animation"); 26 GUILayout.Label("Added *MatchStart* and *MatchEnd* parameters to the Animator Controller"); 27 GUILayout.Label("Added *MatchStart* and *MatchEnd* additionnal curves to the Idle_ToJumpUpHigh animation"); 28 GUILayout.Label("*MatchStart* and *MatchEnd* tell the system when to start cheating the root using MatchTarget API"); 29 GUILayout.Label("Added a RightHand model child of the MoveThis container, to tell where the right hand should go"); 30 GUILayout.Label("On the update function, we call MatchTarget"); 31 GUILayout.Label("Translate the MoveThis object and see how character's hand always reach it"); 32 } 33 34 // Update is called once per frame 35 void Update () 36 { 37 38 if (animator) 39 { 40 AnimatorStateInfo state = animator.GetCurrentAnimatorStateInfo(0); 41 42 if (Input.GetButton("Fire1")) animator.SetBool("Jump", true); 43 44 if (state.IsName("Base Layer.JumpUp") || state.IsName("Base Layer.FullJump")) 45 { 46 animator.SetBool("Jump", false); 47 48 animator.MatchTarget( 49 RightHand.position, 50 RightHand.rotation, 51 AvatarTarget.RightHand, 52 new MatchTargetWeightMask(new Vector3(1, 1, 1), 0), 53 /// 动画 曲线 获取 54 animator.GetFloat("MatchStart"), 55 animator.GetFloat("MatchEnd")); 56 hasJumped = true; 57 } 58 59 if (hasJumped && state.normalizedTime > 1.2) 60 { 61 hasJumped = false; 62 63 Application.LoadLevel(0); 64 } 65 66 Debug.Log(" animator.GetFloat MatchStart " + animator.GetFloat("MatchStart") + " state.normalizedTime " + state.normalizedTime); 67 } 68 69 } 70 }