Unity相机限制

huangapple go评论63阅读模式
英文:

Unity Camera Clamping

问题

我想让我的Unity摄像机在第一人称游戏中被限制在大约-7085之间,但无论我如何将摄像机限制在这些位置之间,它都无法旋转。我尝试尝试了一些不同的值,但似乎没有修复问题,只是让我的摄像机抖动。我还在学习C#,所以任何帮助将不胜感激。谢谢!

```using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraMovement : MonoBehaviour
{
    public float CameraSpeed = 5;
    public Vector3 CamMovement;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        CamMovement = new Vector3(Input.GetAxis("Mouse Y"), 0, 0);
        CamMovement.y = Mathf.Clamp(CamMovement.y, -88, 88);

        transform.Rotate(-CamMovement.y * CameraSpeed, 0, 0);
    }
}
英文:

I want to make my Unity Camera for a first person game to be clamped between around -70ish maximum, 85 minimum, however whenever I clamp my camera between these spots, it will not rotate. I tried messing around with some different values, however that did not seem to fix anything or just made my camera jitter around. I am still learning C#, so any help would be appreciated. Thanks!

using System.Collections.Generic;
using UnityEngine;

public class CameraMovement : MonoBehaviour
{
    public float CameraSpeed = 5;
    public Vector3 CamMovement;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        CamMovement = new Vector3(Input.GetAxis("Mouse Y"), 0, 0);
        CamMovement.y = Mathf.Clamp(CamMovement.y, -88, 88);

        transform.Rotate(-CamMovement.y * CameraSpeed, 0, 0);
    }
}

答案1

得分: 0

transform.Rotate 旋转当前旋转度数。

=> 这样只会限制每帧内旋转的量,而不是最终结果的旋转!

你应该存储旋转的量,然后执行如下操作:

private float rotationX;

void Update()
{
    var input = Input.GetAxis("Mouse Y");
    rotationX += input * CameraSpeed;
    rotationX = Mathf.Clamp(rotationX, -88, 88);

    transform.rotation = Quaternion.Euler(-rotationX, 0, 0);
}

或者如果你需要相对于任意初始旋转:

private float rotationX;
private Quaternion initialRotation;

private void Start()
{
    initialRotation = transform.localRotation;
}

private void Update()
{
    var input = Input.GetAxis("Mouse Y");
    rotationX += input * CameraSpeed;
    rotationX = Mathf.Clamp(rotationX, -88, 88);

    transform.localRotation = initialRotation * Quaternion.Euler(-rotationX, 0, 0);
}
英文:

transform.Rotate

rotates from the current rotation about the given degrees.

=> You are thereby only clamping the amount of rotation that can happen within one frame, not the overall resulting rotation!

You should rather store the rotated amount and do e.g.

private float rotationX;

void Update()
{
    var input = Input.GetAxis("Mouse Y");
    rotationX += input * CameraSpeed;
    rotationX = Mathf.Clamp(rotationX , -88, 88);

    transform.rotation = Quaternion.Euler(-rotationX, 0, 0);
}

or if you need this relative to an arbitrary initial rotation

private float rotationX;
private Quaternion initialRotation;

private void Start()
{
    initialRotation = transform.localRotation;
}

private void Update()
{
    var input = Input.GetAxis("Mouse Y");
    rotationX += input * CameraSpeed;
    rotationX = Mathf.Clamp(rotationX , -88, 88);

    transform.localRotation = initialRotation * Quaternion.Euler(-rotationX, 0, 0);
}

huangapple
  • 本文由 发表于 2023年7月13日 22:04:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定