【问题标题】:How to limit camera's vertical rotation Unity 3D如何限制相机的垂直旋转Unity 3D
【发布时间】:2019-08-10 08:53:11
【问题描述】:

我想限制相机的垂直旋转,使其无法进行 360 度旋转。我已经尝试了很多教程,但没有什么对我有用,所以我。

请检查我的代码。

[RequireComponent(typeof(PlayerMoto))]

public class PlayerController: MonoBehaviour {

  public float speed = 5, sensetivity;
  private PlayerMoto motor;
  public GameObject hands;
  public camera cam;
  float lookUpMax = .6 f;

  void Start() {
    motor = GetComponent < PlayerMoto > ();
    cam = GetComponent < camera > ();
  }

  void Update() {
    //cam.transform.localEulerAngles = new Vector3(cam.transform.localEulerAngles.x, 0, 0);
    float _xmove = Input.GetAxis("Horizontal");
    float _zmove = Input.GetAxis("Vertical");
    Vector3 _moveHorizontal = transform.right * _xmove;
    Vector3 _movVertical = transform.forward * _zmove;
    Vector3 _velocity = (_moveHorizontal + _movVertical) * speed;
    motor.Move(_velocity);
    float _yRot = Input.GetAxis("Mouse X");
    Vector3 _rotation = new Vector3(0 f, _yRot, 0 f) * sensetivity;
    motor.Rotate(_rotation);
    float _xRot = Input.GetAxis("Mouse Y");
    Vector3 _cameraRotation = new Vector3(_xRot, 0 f, 0 f) * sensetivity;
    motor.RotateCamera(_cameraRotation);
    if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W)) {
      for (; speed <= 15; speed++) {
        speed = 15;
      }
    } else {
      speed = 10;
    }
  }
}

非常感谢您的热心帮助。我非常感谢每一条评论都在尝试帮助我度过这段美妙的旅程。

【问题讨论】:

    标签: c# unity3d camera


    【解决方案1】:

    试试这个

     private void Rotation()
        {
            x_axis += speed * Input.GetAxis("Mouse X"); // speed = 2f;
    
            y_axis -= speed * Input.GetAxis("Mouse Y");
            y_axis = Mathf.Clamp (y_axis, -45, 45); // limits vertical rotation
    
            transform.eulerAngles = new Vector3(y_axis, x_axis, 0.0f);
        }
    

    【讨论】:

    • 我真的需要知道你的问题是什么。此外,您需要为我们提供更多背景信息。例如,我们正在处理什么语言以及什么操作系统?
    【解决方案2】:

    如果我理解你的代码,移动相机的部分是:

    float _xRot = Input.GetAxis("Mouse Y");
    Vector3 _cameraRotation = new Vector3(_xRot, 0f, 0f) * sensetivity;
    

    然后,您的代码在另一个类上调用此方法

    motor.RotateCamera(_cameraRotation);
    

    这可能(因为之前的建议不起作用)通过

    应用轮换
    GameObject.Rotate(_cameraRotation);
    

    为了限制你的相机的旋转,我们需要直接应用旋转,如果通过在现有旋转上添加旋转来应用旋转,则无法钳制。 所以,让我们假设您可以跳过该调用并直接应用旋转(我不知道是否会有副作用),并且让我们假设您的 cam 变量是你的相机。 如果所有这些假设都正确,您可以尝试:

    float _xRot += Input.GetAxis("Mouse Y") * sensetivity;
    xRot = Mathf.Clamp(_xRot, xMin, xMax);
    cam.transform.rotation = Quaternion.Euler(_xRot, 0.0f, 0.0f);
    

    记得注释掉

    //motor.RotateCamera(_cameraRotation);
    

    你可以声明

    public float xMin;
    public float xMax;
    

    并尝试各种值,直到找到最佳值。

    我强烈怀疑我建议的代码不会解决您的所有问题,或者它可能会增加问题,因为您的播放器和相机的实际转换是由另一个脚本应用的,没有提供。在这种情况下,您也可以向我们提供该代码,但我建议您尝试编写自己的代码,您可以根据自己的需要进行自定义。

    至于为什么夹紧旋转不像看起来那么容易,这篇文章很有趣: Rotations, Quaternions and Euler Angles

    【讨论】:

    • tnx 试图帮助我,但它仍然不起作用。如果我让 xMax = 60 和 xMin = -60 什么都没有发生,它仍然没有限制,但如果我做 xMax = -60 和 xMin =60 它只是开始旋转相机,我无法停止它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    相关资源
    最近更新 更多