Unity 3D - 限制相机旋转

时间:2017-05-11 16:27:38

标签: c# unity3d unityscript

我在网上找到了几个问题的解决方案,我已经尝试了所有这些,但他们要么打破我的相机,要么只是整体不工作。

这是我的剧本:

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);
}
}

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

解决方案1:将上面的脚本转换为Unityscript(我对C#没有任何帮助)我可以用&#34来解决问题;如果"语句。

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

解决方案3:以上所有

3 个答案:

答案 0 :(得分:2)

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

修改this以限制你的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);
}

答案 1 :(得分:1)

你没有张贴你尝试过的东西,所以这是一个帮助你的黑暗镜头。检查Unity's 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);

答案 2 :(得分:0)

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

相关问题