近期做项目时遇到需要实现摄像机视野拉近拉远的效果
用鼠标滚轮实现摄像机视野拉近拉远
代码如下:
private float mouseX;
private float mouseY;
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (Camera.main.fieldOfView <= 100)
this.GetComponent<Camera>().fieldOfView++;
// 摄像机的正交投影
if (Camera.main.orthographicSize <= 20)
this.GetComponent<Camera>().orthographicSize += 0.5f;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (this.GetComponent<Camera>().fieldOfView > 1)
this.GetComponent<Camera>().fieldOfView--;
if (this.GetComponent<Camera>().orthographicSize >= 1)
this.GetComponent<Camera>().orthographicSize -= 0.5f;
}
}