Unity瞄准镜效果 Unity实现瞄准镜效果
fanfan_hongyun 人气:0想了解Unity实现瞄准镜效果的相关内容吗,fanfan_hongyun在本文为您仔细讲解Unity瞄准镜效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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更改倍镜倍数
加载全部内容