本文实例为大家分享了Unity实现瞄准镜效果的具体代码,供大家参考,具体内容如下
using UnityEngine;
using System.Collections;
public class TelesopicView : MonoBehaviour
{
public float zoomLevel = 2.0f;
public float zoomInSpeed = 100.0f;
public float zoomOutSpeed = 100.0f;
private float initFOV;
public GameObject obj;
void Start()
{
//获取当前摄像机的视野范围 unity默认值60
initFOV = Camera.main.fieldOfView;
}
void Update()
{
if (Input.GetMouseButton(0))
{
ZoomInView();
//激活ui窗口
obj.SetActive(true);
}
else
{
ZoomOutView();
//失活ui窗口
obj.SetActive(false);
}
}
//放大摄像机的视野区域
void ZoomInView()
{
if (Mathf.Abs(Camera.main.fieldOfView - (initFOV / zoomLevel)) < 0f)
{
Camera.main.fieldOfView = initFOV / zoomLevel;
}
else if (Camera.main.fieldOfView - (Time.deltaTime * zoomInSpeed) >= (initFOV / zoomLevel))
{
Camera.main.fieldOfView -= (Time.deltaTime * zoomInSpeed);
}
}
//缩小摄像机的视野区域
void ZoomOutView()
{
if (Mathf.Abs(Camera.main.fieldOfView - initFOV) < 0f)
{
Camera.main.fieldOfView = initFOV;
}
else if (Camera.main.fieldOfView + (Time.deltaTime * zoomOutSpeed) <= initFOV)
{
Camera.main.fieldOfView += (Time.deltaTime * zoomOutSpeed);
}
}
}
此脚本挂到unity主相机即可,可通过更改zoomLevel更改倍镜倍数。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。