[Unity動畫]04.Avatar Mask


參考鏈接:

https://www.cnblogs.com/hammerc/p/4832637.html

 

Avatar Mask主要用於動畫層融合。例如說,邊跑邊舉起東西,這個實際上就是下半身播放跑步動畫,上半身播放舉起動畫,所謂的Avatar Mask就是播放動畫時讓身體的某些部位不起作用(即不播放動畫)

 

1.添加一個新的動畫層

其中Empty是一個空的動畫,同時添加一個Trigger型參數wave

 

TestAnimator.cs

 1 using UnityEngine;
 2 
 3 public class TestAnimator : MonoBehaviour {
 4 
 5     //------------------------------------------外部
 6     public float Move = 0;
 7     public bool IsDying = false;
 8 
 9     //------------------------------------------內部
10     private Animator animator;
11 
12     //常數
13     private int baseLayerIndex;
14     private int idleStateHash;
15     private int runStateHash;
16     private int dyingStateHash;
17     private string movePara;
18     private string isDyingPara;
19     private string wavePara;
20 
21     void Start ()
22     {
23         animator = GetComponent<Animator>();
24 
25         baseLayerIndex = animator.GetLayerIndex("Base Layer");
26         idleStateHash = Animator.StringToHash("Base Layer.Idle");
27         runStateHash = Animator.StringToHash("Base Layer.Run");
28         dyingStateHash = Animator.StringToHash("Base Layer.Dying");
29         movePara = "move";
30         isDyingPara = "isDying";
31         wavePara = "wave";
32     }
33     
34     void Update ()
35     {
36         //------------------------------------------播放動作
37         animator.SetFloat(movePara, Move);
38         if (IsDying)
39         {
40             animator.SetBool(isDyingPara, true);
41             IsDying = false;
42         }
43 
44         //------------------------------------------動作恢復
45         AnimatorStateInfo stateInfo;
46         int fullPathHash;
47 
48         //BaseLayer
49         stateInfo = animator.GetCurrentAnimatorStateInfo(baseLayerIndex);
50         if (!animator.IsInTransition(baseLayerIndex) && stateInfo.normalizedTime >= 1)
51         {
52             fullPathHash = stateInfo.fullPathHash;
53             if (fullPathHash == dyingStateHash)
54             {
55                 animator.SetBool(isDyingPara, false);
56             }
57         }
58     }
59 
60     public void SetDying(bool state)
61     {
62         if (animator)
63         {
64             animator.SetBool(isDyingPara, state);
65         }
66     }
67 
68     public void TriggerWave()
69     {
70         if (animator)
71         {
72             animator.SetTrigger(wavePara);
73         }
74     }
75 
76     public void ActionCallBack(string s)
77     {
78         if (s == "dyingStart")
79         {
80             Debug.Log("111");
81         }
82         else if (s == "dying")
83         {
84             Debug.LogWarning("222");
85         }
86         else if (s == "dyingEnd")
87         {
88             Debug.LogError("333");
89         }
90     }
91 }

 

NewBehaviourScript.cs

 1 using UnityEngine;
 2 
 3 public class NewBehaviourScript : MonoBehaviour {
 4 
 5     public TestAnimator testAnimator;
 6         
 7     void Update ()
 8     {
 9         if (Input.GetKeyDown(KeyCode.Q))
10         {
11             testAnimator.Move = 1;
12         }
13         if (Input.GetKeyDown(KeyCode.W))
14         {
15             testAnimator.TriggerWave();
16         }
17     }
18 }

 

效果如下。按下Q,播跑步動畫,再按下W,覆蓋為招手動畫。可以試着將這兩個層的位置換一下。可以看出,動畫層越后優先級越高;同時還受動畫層的權重影響。

 

2.創建一個Avatar Mask並添加到動畫層中

如下圖2,表示該動畫層中只有上半身起作用

 

效果如下。按下Q,播跑步動畫,再按下W,上半身播招手動畫。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM