unity官方demo學習之Stealth(十五)單開門動畫
阿新 • • 發佈:2019-02-07
1,將modles中的door_generic_slide拖入層級檢視,位置:-6,0,7;角度:90.子物件勾選 use light probes使光可以照到門
2,父物件新增Sphere collider使門可以檢測到敵人或玩家的靠近,center:y:1,radius:3,勾選 Is Trigger ;子物件新增box collider,不用調整
3,為門新增開關動畫,將DoneSingleDoorAnimator拖動到Collider裡,取消Apply Root Motion(如果要自己製作該動畫的話,
將modles中的door_generic_slide下的close先拖進去(因為預設狀態使關閉),再拖進open,新增bool型別引數Open,設定close與open之間的轉換
條件是Open引數,為使動畫更平滑,分別拖動兩條線到150%)
4,門移動過程中,碰撞器也在移動,所以要為其新增剛體元件,但不能讓他影響碰撞,所以勾選Is Kinematic,取消 use Gravity。門要播放音效,新增Audio->audio source元件
引數:(公有)判斷門開啟是否需要鑰匙(單門需要,電梯的雙門不需要)
門開啟或關閉時的音效
玩家沒有鑰匙時門播放音效
(私有)
動作控制器引用
HashID指令碼引用
玩家物件引用
玩家財產指令碼引用
用來計數門觸發器周圍的碰撞數目(來決定門的開關)
函式: Awake
獲取引數中建立的4個引用
OnTriggerEnter (Collider other)進入觸發器時觸發
如果碰撞物件是玩家
門是否需要鑰匙
如果玩家有鑰匙
計數++(門可以開啟)
玩家沒鑰匙
設定音訊剪輯為accessDeniedClip
播放一次沒鑰匙時的音效
門不需要鑰匙
計數++
如果碰撞物件時敵人
如果碰撞到敵人的CapsuleCollider(用is關鍵字來判斷)
計數++
OnTriggerExit (Collider other)離開觸發器時觸發
離開的是玩家或敵人的CapsuleCollider
計數器返回0 count = Mathf.Max(0, count-1);
Update
根據計數器返回Open的bool值
如果門在移動且沒有播放音效(0代表base layer)
設定音訊剪輯為doorSwishClip
播放一次該音訊剪輯
6,將door_open,door_accessDenied分別拖入Door Swish Clip和Access Denied Clip
7,將門door_generic_slide製作成prefab
8,Ctrl+d複製一個,位置-15.9,0,7; 再一個,-7.9,0.37,;
2,父物件新增Sphere collider使門可以檢測到敵人或玩家的靠近,center:y:1,radius:3,勾選 Is Trigger ;子物件新增box collider,不用調整
3,為門新增開關動畫,將DoneSingleDoorAnimator拖動到Collider裡,取消Apply Root Motion(如果要自己製作該動畫的話,
將modles中的door_generic_slide下的close先拖進去(因為預設狀態使關閉),再拖進open,新增bool型別引數Open,設定close與open之間的轉換
條件是Open引數,為使動畫更平滑,分別拖動兩條線到150%)
4,門移動過程中,碰撞器也在移動,所以要為其新增剛體元件,但不能讓他影響碰撞,所以勾選Is Kinematic,取消 use Gravity。門要播放音效,新增Audio->audio source元件
5,為門新增指令碼DoneDoorAnimation控制門的開關
using UnityEngine; using System.Collections; public class DoneDoorAnimation : MonoBehaviour { public bool requireKey; // Whether or not a key is required. public AudioClip doorSwishClip; // Clip to play when the doors open or close. public AudioClip accessDeniedClip; // Clip to play when the player doesn't have the key for the door. private Animator anim; // Reference to the animator component. private DoneHashIDs hash; // Reference to the HashIDs script. private GameObject player; // Reference to the player GameObject. private DonePlayerInventory playerInventory; // Reference to the PlayerInventory script. private int count; // The number of colliders present that should open the doors. void Awake () { // Setting up the references. anim = GetComponent<Animator>(); hash = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent<DoneHashIDs>(); player = GameObject.FindGameObjectWithTag(DoneTags.player); playerInventory = player.GetComponent<DonePlayerInventory>(); } void OnTriggerEnter (Collider other) { // If the triggering gameobject is the player... if(other.gameObject == player) { // ... if this door requires a key... if(requireKey) { // ... if the player has the key... if(playerInventory.hasKey) // ... increase the count of triggering objects. count++; else { // If the player doesn't have the key play the access denied audio clip. audio.clip = accessDeniedClip; audio.Play(); } } else // If the door doesn't require a key, increase the count of triggering objects. count++; } // If the triggering gameobject is an enemy... else if(other.gameObject.tag == DoneTags.enemy) { // ... if the triggering collider is a capsule collider... if(other is CapsuleCollider) // ... increase the count of triggering objects. count++; } } void OnTriggerExit (Collider other) { // If the leaving gameobject is the player or an enemy and the collider is a capsule collider... if(other.gameObject == player || (other.gameObject.tag == DoneTags.enemy && other is CapsuleCollider)) // decrease the count of triggering objects. count = Mathf.Max(0, count-1); } void Update () { // Set the open parameter. anim.SetBool(hash.openBool,count > 0); // If the door is opening or closing... if(anim.IsInTransition(0) && !audio.isPlaying) { // ... play the door swish audio clip. audio.clip = doorSwishClip; audio.Play(); } } }
引數:(公有)判斷門開啟是否需要鑰匙(單門需要,電梯的雙門不需要)
門開啟或關閉時的音效
玩家沒有鑰匙時門播放音效
(私有)
動作控制器引用
HashID指令碼引用
玩家物件引用
玩家財產指令碼引用
用來計數門觸發器周圍的碰撞數目(來決定門的開關)
函式: Awake
獲取引數中建立的4個引用
OnTriggerEnter (Collider other)進入觸發器時觸發
如果碰撞物件是玩家
門是否需要鑰匙
如果玩家有鑰匙
計數++(門可以開啟)
玩家沒鑰匙
設定音訊剪輯為accessDeniedClip
播放一次沒鑰匙時的音效
門不需要鑰匙
計數++
如果碰撞物件時敵人
如果碰撞到敵人的CapsuleCollider(用is關鍵字來判斷)
計數++
OnTriggerExit (Collider other)離開觸發器時觸發
離開的是玩家或敵人的CapsuleCollider
計數器返回0 count = Mathf.Max(0, count-1);
Update
根據計數器返回Open的bool值
如果門在移動且沒有播放音效(0代表base layer)
設定音訊剪輯為doorSwishClip
播放一次該音訊剪輯
6,將door_open,door_accessDenied分別拖入Door Swish Clip和Access Denied Clip
7,將門door_generic_slide製作成prefab
8,Ctrl+d複製一個,位置-15.9,0,7; 再一個,-7.9,0.37,;