unity鼠标滚轮控制摄像机视野的缩放和按住鼠标控制摄像机移动

//摄像机前进后退的速率
private float view_value=20f;
private float maximum = 100;
private float minmum = 30;

//滚轮实现镜头缩进和拉远的范围
private float sensitivetyMouseWheel = 10f;
//控制摄像机移动的速率
private float move_speed=1f;

void Update()
{
  //摄像机在Z轴实现前进后退的效果
  if (Input.GetAxis("Mouse ScrollWheel") != 0)
  {
     Camera.main.transform.Translate(new Vector3(0, 0, Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * view_value));
  }

  //滚轮实现摄像机视角的缩进和放远 
  if (Input.GetAxis("Mouse ScrollWheel") != 0)
  {

    Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView, minmum, maximum);
    Camera.main.fieldOfView = Camera.main.fieldOfView - Input.GetAxis("Mouse ScrollWheel") * view_value;

  }
  //移动视角
  if (Input.GetMouseButton(0))
  {
    transform.Translate(Vector3.left * Input.GetAxis("Mouse X") * move_speed);
    transform.Translate(Vector3.up * Input.GetAxis("Mouse Y") * -move_speed);
  }
}

posted @ 2017-08-07 12:59  U3DEngineer  阅读(2093)  评论(0编辑  收藏  举报