【问题标题】:Unity 3D - Limit Camera RotationUnity 3D - 限制相机旋转
【发布时间】:2021-09-09 05:22:48
【问题描述】:

我在网上找到了几个解决这个问题的遮阳篷,我都试过了,但它们要么坏了我的相机,要么就是整体不起作用。

这是我的脚本:

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:以上所有方法

【问题讨论】:

  • 如果你不知道如何在 c# 中使用 if 语句,你应该先集中精力学习基础知识,花一两天时间。

标签: c# unity3d


【解决方案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】:

没有尝试限制代码中的任何轴。使用一个临时变量来限制您的轴,方法是在每个 tome 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);
}

【讨论】:

  • 这个脚本在我的游戏中无效
  • 我做到了。脚本没有做任何事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-23
  • 2012-01-17
  • 1970-01-01
相关资源
最近更新 更多