unity-飞行控制

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

usingUnityEngine;usingSystem.Collections;usingSystem.Collections.Generic;publicclassFPSFlyHao:MonoBehaviour{publicfloatflyTime=1f;publicfloatdeley=0f;//摄像机快狗模式,镜头直达目标位置。publicboolquickCam=false;//是否需要限制底部publicboolIsBottom;//publicVector3bestFactor=newVector3(1f,1f,2f);publicdelegatevoidFlyEndDelegate();publicTransformtarget;///summary///摄像机仰视角度的范围////summarypublicVector2pitchLimit=newVector2(0,65f);publicVector2zoomLimit=newVector2(0.5f,200f);publicVector2heightLimit=newVector2(0f,200f);publicVector2rotSpeed=newVector2(150f,50f);publicfloatrotDamping=10f;publicfloatpanSpeed=1f;publicfloatpanDamping=5f;publicfloatzoomSpeed=2f;publicfloatzoomDamping=8f;publicVector3movSpeed=newVector3(2f,2f,2f);publicboolsmooth=true;publicGameObjectball;publicTransformfollowTarget;publicfloatdefaultRadius;publicboolisControlling;publicboolenablePan=true;publicboolenableRot=true;publicboolenableMove=true;publicboolenableZoom=true;publicboolenableZoomInput=true;publicboolenablePanInput=true;privateCameracam;///summary///转向背后////summaryprivatebooloff=false;///summary///摄像机的欧拉角y值////summaryprivatefloatx;///summary///摄像机的欧拉角x值////summaryprivatefloaty;privatefloatwantedX;privatefloatwantedY;///summary/////摄像机和target的距离////summarypublicfloatdistance=10f;privatefloatwantedDistance=10f;privatefloatpanX;privatefloatpanY;privatefloatwantedPanX;privatefloatwantedPanY;///summary///鼠标左键按下了,可以进行旋转操作////summaryprivatebooldragRot;///summary///鼠标右键按下了,可以进行拖动操作////summaryprivatebooldragPan;privateVector2rotValue=newVector2(0f,0f);privateVector2panValue=newVector2(0f,0f);privatefloatzoomValue;///summary///镜头移动距离////summaryprivateVector3moveOffset=newVector3(1f,1f,1f);privateFlyEndDelegateFlyEndCallback;privatefloatwaitTime;publicboolPause{//暂停get;set;}publicboolFlying{//运动get;set;}publicfloatWaitTime{//get{returnwaitTime;}set{waitTime=value;}}privatevoidAwake(){if(!target){print(Notarget);return;}}privatevoidStart(){cam=gameObject.camera;if(ball){ball.renderer.enabled=false;LayerMaskmask=LayerMask.NameToLayer(IgnoreRaycast);ball.layer=mask;}if(target){ResetByTarget();}}publicvoidResetByTarget(){dragRot=false;//拖拽的角度dragPan=false;////摄像机和target的距离distance=(wantedDistance=Vector3.Distance(transform.position,target.position));Vector3eulerAngles=transform.eulerAngles;x=(wantedX=eulerAngles.y);y=(wantedY=eulerAngles.x);panX=(panY=0f);wantedPanX=(wantedPanY=0f);if(enableRot){UpdateRotate(0f,0f,0f);}}privatevoidLateUpdate(){if(!target){return;}if(followTarget){target.position=followTarget.position;}//当鼠标按下点击了界面上一个控件时,返回if(GUIUtility.hotControl!=0){return;}if(Flying)//如果镜头在飞行过程中,则镜头始终面对目标{gameObject.transform.LookAt(target.position,Vector3.up);return;}floatmaxWaitTime=Time.deltaTime;if(maxWaitTime0.05f){maxWaitTime=0.05f;}if(waitTime0f){waitTime-=maxWaitTime;return;}if(!Pause){UpdateInputOnPC(maxWaitTime);}if(enableRot){UpdateRotate(rotValue.x,rotValue.y,maxWaitTime);}if(followTarget==null&&enablePan){UpdatePan(panValue.x,panValue.y,maxWaitTime);}if(enableZoom){UpdateZoom(zoomValue,maxWaitTime);}if(followTarget==null&&enableMove){UpdateMove(moveOffset.x,moveOffset.y,moveOffset.z,maxWaitTime);}}privatevoidUpdateBall(){if(!ball){return;}if(Input.GetKey(KeyCode.LeftControl)||Input.GetKey(KeyCode.RightControl)){ball.renderer.enabled=true;RaycastHitraycastHit;if(Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition),outraycastHit)){ball.transform.position=raycastHit.point;}}else{ball.renderer.enabled=false;}if(Input.GetKeyUp(KeyCode.LeftControl)||Input.GetKeyUp(KeyCode.RightControl)){Quaternionrotation=Quaternion.Euler(y,x,0f);floatd=Vector3.Distance(ball.transform.position,cam.transform.position);Vector3destPos=ball.transform.position+rotation*Vector3.back*d;FlyTo(destPos,ball.transform.position);}}///summary///根据鼠标和键盘操作,计算出镜头移动距离和旋转角度////summary///paramname=frameTime/paramprivatevoidUpdateInputOnPC(floatframeTime){floataxis=Input.GetAxis(MouseX);floataxis2=Input.GetAxis(MouseY);floataxis3=Input.GetAxis(MouseScrollWheel);Vector2point=newVector2(Input.mousePosition.x/(float)Screen.width,Input.mousePosition.y/(float)Screen.height);boolisInCam=cam.rect.Contains(point);//鼠标是否在窗口内,防止鼠标出屏后操作//print(Input.mousePosition=+Input.mousePosition+,point=+point+,isInCam=+isInCam+,cam.rect=+cam.rect);//鼠标左键按下事件触发,并且鼠标在窗口内if(Input.GetMouseButtonDown(0)&&isInCam){dragRot=true;}//鼠标左键抬起if(Input.GetMouseButtonUp(0)){dragRot=false;}//是否鼠标正在移动进行旋转操作boolrotting=false;//鼠标左键是按下状态if(Input.GetMouseButton(0)&&dragRot){rotting=true;//正在进行旋转操作}//鼠标中键按下if(Input.GetMouseButtonDown(2)&&isInCam){//print(GetMouseButtonDown(2));dragPan=true;}if(Input.GetMouseButtonUp(2)){dragPan=false;}boolpanning=false;//是否能拖动if(Input.GetMouseButton(2)&&dragPan){panning=true;}if(dragPan||dragRot){if(!isControlling&&(!axis.Equals(0f)||!axis2.Equals(0f))){isControlling=true;}}else{isControlling=false;}moveOffset.Set(0f,0f,0f);if(isInCam){floatvSpeed=1f;if(Input.GetKey(KeyCode.RightShift)||Input.GetKey(KeyCode.LeftShift)){vSpeed=2f;}if(Input.GetKey(KeyCode.W)){move

1 / 27
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功