一个控制摄像头移动的脚本,直接绑定到摄像机就可以使用.功能描述如下:
1.鼠标左键拖动观察
2.鼠标右键定点旋转
3.鼠标滚轴放大缩小
4.鼠标左键快速双击进行定点放大
#pragma strict
/***
**摄影机控制
*@author felly
*/
private var rayRotate :Ray = Ray(Vector3.zero, Vector3(0,1,0));
private var rayTranslate :Ray = Ray(Vector3.zero, Vector3(0,1,0));
private var hitInfo : RaycastHit ;
private var preTime :float = 0.0;
//它标记是否能旋转
private var flag :boolean = false ;
function Update () {///事件监视
var direction :Vector3 = Vector3.zero;
var px:float ;
var pz:float ;
if(Input.GetMouseButtonDown(0)){
rayTranslate = camera.ScreenPointToRay(Input.mousePosition);
//以摄像机为起点画射线,射线去地板碰撞点就是那个焦点
Physics.Raycast(camera.transform.position, rayTranslate.direction, hitInfo);
if (Time.time - preTime < 0.3f ){
hitInfo.point=hitInfo.point+Vector3(0,5,0);
transform.position=Vector3.MoveTowards(camera.transform.position,hitInfo.point,10.0);
}
preTime = Time.time ;
}
//上下左右拖动
if(Input.GetMouseButton(0)){
px = Input.GetAxis("Mouse X") ;
pz = Input.GetAxis("Mouse Y") ;
direction = Vector3(px, pz, pz);
transform.Translate( -1 * direction * 30 * Time.deltaTime);
}
//滚轴放到缩小
pz = Input.GetAxis("Mouse ScrollWheel");
direction = Vector3(0, 0, pz);
transform.Translate(direction * 100 * Time.deltaTime);
//旋转
if(Input.GetMouseButtonDown(1)){
rayRotate = camera.ScreenPointToRay(Input.mousePosition);
flag = true ;
}
if(Input.GetMouseButtonUp(1)){
rayRotate = Ray(Vector3.zero, Vector3(0,1,0));
flag = false ;
}
if(Input.GetMouseButton(1) && flag){
Physics.Raycast(camera.transform.position, rayRotate.direction, hitInfo);
px = Input.GetAxis("Mouse X") ;
transform.RotateAround(hitInfo.point, Vector3.up, 100 * px * Time.deltaTime);
}
}