Unity 3D - 限制相机旋转 [英] Unity 3D - Limit Camera Rotation
问题描述
我在网上找到了几个针对这个问题的 awnsers,我已经尝试了所有这些,但它们要么损坏了我的相机,要么总体上不起作用.
I have found several awnsers to this question online, and I have tried all of them, but they either break my camera, or just overall don't work.
这是我的脚本:
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 个解决方案,但我不知道如何实现任何一个解决方案
As far as I know, there is 3 solutions to this problem, but I don't know how to implement any of the solutions
解决方案1:将上面的脚本转换为Unityscript(我对C#几乎没有经验),我可以用if"语句解决问题.
Solution 1: Convert the script above to Unityscript (I have little expirience with C#) and I can solve the problem with "if" statements.
解决方案 2:提供 C# 代码以将我脚本上的角度限制为所有轴 90 度的角度
Solution 2: Provide the C# code to limit the angle on my script to an angle of 90 degrees all axis
解决方案 3:以上所有
Solution 3: All of the above
推荐答案
你没有发布你尝试过的东西,所以这是帮助你的黑暗中的一个镜头.检查 Unity 的 Mathf.Clamp 以限制允许的角度.
You don't post what you've tried, so this is a shot in the dark on helping you. Check Unity's Mathf.Clamp to restrict the angles allowed.
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);
这篇关于Unity 3D - 限制相机旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!