一个控制摄像头移动的脚本,直接绑定到摄像机就可以使用.功能描述如下:
1.鼠标左键拖动观察
2.鼠标右键定点旋转
3.鼠标滚轴放大缩小
4.鼠标左键快速双击进行定点放大
1 #pragma strict 2 /*** 3 **摄影机控制 4 *@author felly 5 */ 6 7 private var rayRotate :Ray = Ray(Vector3.zero, Vector3(0,1,0)); 8 private var rayTranslate :Ray = Ray(Vector3.zero, Vector3(0,1,0)); 9 private var hitInfo : RaycastHit ; 10 11 private var preTime :float = 0.0; 12 13 14 //它标记是否能旋转 15 private var flag :boolean = false ; 16 17 function Update () {///事件监视 18 19 var direction :Vector3 = Vector3.zero; 20 var px:float ; 21 var pz:float ; 22 23 24 if(Input.GetMouseButtonDown(0)){ 25 rayTranslate = camera.ScreenPointToRay(Input.mousePosition); 26 //以摄像机为起点画射线,射线去地板碰撞点就是那个焦点 27 Physics.Raycast(camera.transform.position, rayTranslate.direction, hitInfo); 28 if (Time.time - preTime < 0.3f ){ 29 hitInfo.point=hitInfo.point+Vector3(0,5,0); 30 transform.position=Vector3.MoveTowards(camera.transform.position,hitInfo.point,10.0); 31 } 32 33 preTime = Time.time ; 34 } 35 36 //上下左右拖动 37 if(Input.GetMouseButton(0)){ 38 px = Input.GetAxis("Mouse X") ; 39 pz = Input.GetAxis("Mouse Y") ; 40 direction = Vector3(px, pz, pz); 41 transform.Translate( -1 * direction * 30 * Time.deltaTime); 42 } 43 44 //滚轴放到缩小 45 pz = Input.GetAxis("Mouse ScrollWheel"); 46 direction = Vector3(0, 0, pz); 47 transform.Translate(direction * 100 * Time.deltaTime); 48 49 //旋转 50 if(Input.GetMouseButtonDown(1)){ 51 rayRotate = camera.ScreenPointToRay(Input.mousePosition); 52 flag = true ; 53 } 54 if(Input.GetMouseButtonUp(1)){ 55 rayRotate = Ray(Vector3.zero, Vector3(0,1,0)); 56 flag = false ; 57 } 58 if(Input.GetMouseButton(1) && flag){ 59 Physics.Raycast(camera.transform.position, rayRotate.direction, hitInfo); 60 px = Input.GetAxis("Mouse X") ; 61 transform.RotateAround(hitInfo.point, Vector3.up, 100 * px * Time.deltaTime); 62 } 63 }