1,給第一人稱控制器添加腳本:playercollisions.js 腳本中只定義變數,先不添加方法: #pragma strict var door_open_time:float=3.0; var door_open_sound:AudioClip; var door_shut_sound:Au ...
1,給第一人稱控制器添加腳本:playercollisions.js 腳本中只定義變數,先不添加方法:
#pragma strict var door_open_time:float=3.0; var door_open_sound:AudioClip; var door_shut_sound:AudioClip; private var doorisopen:boolean=false; private var doortimer:float=0.0; private var currentdoor:GameObject;View Code
2,給playercollisions.js中的變數指定對象:
3,然後添加碰撞觸發執行的方法:
function OnControllerColliderHit(hit:ControllerColliderHit){ if (hit.gameObject.tag=="playerDoor" && doorisopen==false) { opendoor(hit.gameObject); } } function opendoor(door:GameObject){ doorisopen=true; door.GetComponent(AudioSource).PlayOneShot(door_open_sound); door.transform.parent.GetComponent(Animation).Play(); }View Code
4,確定碰撞對象已經添加Collider碰撞組件,規則物體(Cube)添加Box Collider,不規則物體添加Mesh Collider:
我們給Door添加Box Collider並增大Size Y ,方便碰撞操作:
5,處理碰撞的時候,可以藉助 print((hit.gameObject); 查看當前碰撞的對象是什麼
是 Door 還是 OutPost:
6,判斷 hit.gameObject.tag=="playerDoor" 的時候,要保證大小寫一致
7,播放開門動畫的時候,註意教程中outPost和自己項目中outPost的Animations的區別:
教程中的動畫進行了分割,而自己項目中的動畫沒有,所以我們只播放一次:
door.transform.parent.GetComponent(Animation).Play();
------
8,5.x版本中的播放方法:
door.GetComponent(AudioSource).PlayOneShot(door_open_sound);
9,碰撞開門,播放聲音的JS代碼,添加到第一人稱後,即可實現碰撞開門、播放開門聲音:
#pragma strict var door_open_time:float=3.0; var door_open_sound:AudioClip; var door_shut_sound:AudioClip; private var doorisopen:boolean=false; private var doortimer:float=0.0; private var currentdoor:GameObject; function OnControllerColliderHit(hit:ControllerColliderHit){ print(hit.gameObject); if (hit.gameObject.tag=="playerDoor" && doorisopen==false) { opendoor(hit.gameObject); } } function opendoor(door:GameObject){ doorisopen=true; door.GetComponent(AudioSource).PlayOneShot(door_open_sound); door.transform.parent.GetComponent(Animation).Play(); }View Code
10,加入關門方法,3秒後再次播放動畫:
#pragma strict var door_open_time:float=3.0; var door_open_sound:AudioClip; var door_shut_sound:AudioClip; private var doorisopen:boolean=false; private var doortimer:float=0.0; private var currentdoor:GameObject; private var currentDoor:GameObject; function Start(){ } function Update(){ //如果門打開,開始計時 超過3秒後 再吃執行動畫,並把開門時間重置為0 if(doorisopen==true){ doortimer+=Time.deltaTime; print(doortimer); if(doortimer>door_open_time){ doortimer=0; shutdoor(currentDoor); } } } //檢測碰撞 function OnControllerColliderHit(hit:ControllerColliderHit){ print(hit.gameObject); if (hit.gameObject.tag=="playerDoor" && doorisopen==false) { currentDoor=hit.gameObject; opendoor(hit.gameObject); } } // //開門 function opendoor(door:GameObject){ doorisopen=true; door.GetComponent(AudioSource).PlayOneShot(door_open_sound); door.transform.parent.GetComponent(Animation).Play(); } // 關門 function shutdoor(door:GameObject){ doorisopen=false; door.GetComponent(AudioSource).PlayOneShot(door_shut_sound); door.transform.parent.GetComponent(Animation).Play(); }View Code
11,整合開門、關門方法:
#pragma strict var door_open_time:float=3.0; var door_open_sound:AudioClip; var door_shut_sound:AudioClip; private var doorisopen:boolean=false; private var doortimer:float=0.0; private var currentdoor:GameObject; private var currentDoor:GameObject; function Start(){ } function Update(){ //如果門打開,開始計時 超過3秒後 再吃執行動畫,並把開門時間重置為0 if(doorisopen==true){ doortimer+=Time.deltaTime; print(doortimer); if(doortimer>door_open_time){ doortimer=0; //shutdoor(currentDoor); door(currentDoor,false,door_shut_sound,"closedoor"); } } } //檢測碰撞 function OnControllerColliderHit(hit:ControllerColliderHit){ print(hit.gameObject); if (hit.gameObject.tag=="playerDoor" && doorisopen==false) { currentDoor=hit.gameObject; //opendoor(hit.gameObject); door(currentDoor,true,door_open_sound,"opendoor"); } } //對象,門的狀態,播放的聲音,播放的對象 function door(thisDoor:GameObject,doorOpenOrClose:boolean,audio_clip:AudioClip,ani_name:String){ doorisopen=doorOpenOrClose; thisDoor.GetComponent(AudioSource).PlayOneShot(audio_clip); thisDoor.transform.parent.GetComponent(Animation).Play(); //播放動畫 區分開門和關門 }View Code