Unity 3D – 限制相机旋转

我在网上找到了几个这个问题的灵感,我已经尝试了所有这些,但他们要么打破我的相机,要么总体上不起作用。

这是我的脚本:

using UnityEngine; using System.Collections; public class fp : MonoBehaviour { public float speedH = 2.0f; public float speedV = 2.0f; private float yaw = 0.0f; private float pitch = 0.0f; void Update() { yaw += speedH * Input.GetAxis("Mouse X"); pitch -= speedV * Input.GetAxis("Mouse Y"); transform.eulerAngles = new Vector3(pitch, yaw, 0.0f); } } 

据我所知,这个问题有3个解决方案,但我不知道如何实现任何解决方案

解决方案1:将上面的脚本转换为Unityscript(我对C#没什么看法),我可以用“if”语句解决问题。

解决方案2:提供C#代码,将我脚本上的角度限制为所有轴的90度角

解决方案3:以上所有

没有尝试限制代码中的任何轴。 使用临时变量来限制轴,通过递增每个tome Input.GetAxis更改。 如果达到最小值或最大值, Mathf.Clamp将其限制为然后Mathf.Clamp将其限制在最小值和最大值/角度之间。

修改此项以限制您的FPS相机在两个轴而不是通常的y轴限制。

 public float xMoveThreshold = 1000.0f; public float yMoveThreshold = 1000.0f; //Y limit public float yMaxLimit = 45.0f; public float yMinLimit = -45.0f; float yRotCounter = 0.0f; //X limit public float xMaxLimit = 45.0f; public float xMinLimit = -45.0f; float xRotCounter = 0.0f; Transform player; void Start() { player = Camera.main.transform; } // Update is called once per frame void Update() { //Get X value and limit it xRotCounter += Input.GetAxis("Mouse X") * xMoveThreshold * Time.deltaTime; xRotCounter = Mathf.Clamp(xRotCounter, xMinLimit, xMaxLimit); //Get Y value and limit it yRotCounter += Input.GetAxis("Mouse Y") * yMoveThreshold * Time.deltaTime; yRotCounter = Mathf.Clamp(yRotCounter, yMinLimit, yMaxLimit); //xRotCounter = xRotCounter % 360;//Optional player.localEulerAngles = new Vector3(-yRotCounter, xRotCounter, 0); } 

你没有张贴你尝试过的东西,所以这是一个帮助你的黑暗镜头。 检查Unity的Mathf.Clamp以限制允许的角度。

 yaw += speedH * Input.GetAxis("Mouse X"); pitch -= speedV * Input.GetAxis("Mouse Y"); yaw = Mathf.Clamp(yaw, -90f, 90f); pitch = Mathf.Clamp(pitch, -60f, 90f); transform.eulerAngles = new Vector3(pitch, yaw, 0.0f); 

您是否试图限制相机的旋转以适应第一人称角色控制器,以便通过鼠标移动控制相机?