解決VR頭盔下原本一般鏡頭縮放程式碼異常調整後指令,此程式碼放置於場景攝影機上,可透過滑鼠滾輪縮放鏡頭。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class 我是程式碼檔名 : MonoBehaviour
{
//鏡頭前後縮放速度
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);
}
}
}
留言列表