unity官方demo学习之Stealth(十五)单开门动画

来源:互联网 发布:云墙是什么软件 编辑:程序博客网 时间:2024/04/24 06: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组件

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,;


0 0
原创粉丝点击