Unity3D 5.x 交互功能 - 碰撞检测函数
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;
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(); }
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(); }
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(); }
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(); //播放动画 区分开门和关门 }