using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 镜头缩放
///
public class CareamZoom : MonoBehaviour
{
public bool isDown;
public bool keyDown;
public bool cvDown;
public Transform l1;
public bool isFar = true;
private Camera camera;
public int[] zoomLevel;
private int index;
private Vector3 lo;
private Quaternion ro;
private void Start()
{
camera = GetComponent<Camera>();
lo = this.transform.position;
ro = this.transform.rotation;
}
private void Update()
{
isDown = Input.GetMouseButton(0);//左键
keyDown = Input.GetKey(KeyCode.Q);
CamerPosition();
if (keyDown)
Zoom4();
cvDown = Input.GetKey(KeyCode.C) && Input.GetKey(KeyCode.V);
}
private void CamerPosition()
{
if (keyDown)
{
this.transform.position = this.transform.parent.position;
this.transform.rotation = this.transform.parent.rotation;
}
else
{
this.transform.position = l1.position;
this.transform.rotation = l1.rotation;
index = 0;
camera.fieldOfView = zoomLevel[index ];
}
}
private void Zoom1()
{
if (Input.GetMouseButtonDown(1))
{
//拉近:10
isFar = !isFar;
if (isFar)
{
camera.fieldOfView = 50;
//拉远:50
}
else
{
camera.fieldOfView = 10;
}
}
}
private void Zoom2()
{
if (Input.GetMouseButtonDown(1))
{
isFar = !isFar;
}
if (isFar)
{
if (camera.fieldOfView < 50)
camera.fieldOfView += 2;
//拉远:10-->50
}
else
{
//拉近:50-->10
if (camera.fieldOfView > 10)
camera.fieldOfView -= 2;
}
}
private void Zoom3()//先快后慢 Lerp (起点,终点,比例0.1)第一次返回起点到终点0.1的值
{
if (Input.GetMouseButtonDown(1))
{
isFar = !isFar;
}
if (isFar)
{
//拉远:10-->50
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 50, 0.1f);//无限接近50,不等于
if (Mathf.Abs(camera.fieldOfView - 50) < 0.1) camera.fieldOfView = 50;//使无限接近时,等于
}
else
{
//拉近:50-->10
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 10, 0.1f);
if (Mathf.Abs(camera.fieldOfView - 10) < 0.1) camera.fieldOfView = 10;
}
}
private void Zoom4()//
{
if (Input.GetMouseButtonDown(1))
{
//index = index < zoomLevel.Length - 1 ? index + 1 : 0;
index = (index + 1) % zoomLevel.Length;
//index++;
//if (index >= zoomLevel.Length)
//index = 0;
}
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoomLevel[index], 0.1f);//无限接近50,不等于
if (Mathf.Abs(camera.fieldOfView - zoomLevel[index]) < 0.1) camera.fieldOfView = zoomLevel[index];//使无限接近时,等于
}
}