【问题标题】:Limit Player Rotation Up and Down限制玩家上下轮换
【发布时间】:2019-08-01 23:12:17
【问题描述】:

我有基本的移动和旋转工作,但是我无法找到限制上下旋转的方法。我想让它向上和向下看不能超过 90°。

我尝试了多种方法,例如使用 if 语句和使用钳位。

使用 UnityEngine;

公共类 FPSController : MonoBehaviour {

public float speed = 5f;
public float sensitivity = 2f;
public GameObject Camera;

CharacterController controller;

float moveFB;
float moveLR;

public float rotX;
public float rotY;



void Start()
{
    controller = GetComponent<CharacterController>();
    Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate ()
{
    moveFB = Input.GetAxis("Vertical");
    moveLR = Input.GetAxis("Horizontal");

    rotX = Input.GetAxis("Mouse X") * sensitivity;
    rotY = Input.GetAxis("Mouse Y") * sensitivity;


    transform.Rotate(0, rotX, 0);
    Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
    controller.Move(transform.rotation * movement);
    Camera.transform.Rotate(-rotY, 0, 0);

}

}

使用此代码,您将能够将相机旋转超过 90 度,使其倒置等

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    “Camera”是一个内置的统一类,我建议将其重命名为“camera”。 试试这个来限制相机的旋转:
    (与您的其他公共花车)

    public float minAngle = -90;
    public float maxAngle = 90;
    

    (在固定更新结束时)

    Vector3 temp = camera.transform.localEulerAngles;
    camera.transform.localEulerAngles = new Vector3(Mathf.Clamp(Mathf.DeltaAngle(0, temp.x), minAngle, maxAngle), temp.y, temp.z);
    

    编辑:将 eulerAngles 更改为 localEulerAngles
    编辑 2:更改了 Mathf.DeltaAngle 的争论顺序

    【讨论】:

    • 我不知道我是否正确添加了它,但是当我尝试它时,当你一直看向地面或仰望天空时,相机会有点抽搐。
    • 我的错,我认为应该是 camera.transform.localEulerAngles(用这个替换两个欧拉角)
    • 我已经将其更改为本地。该代码确实会阻止您将相机旋转 90 度以上,但它会卡在那里并导致其他问题
    • 尝试用Mathf.DeltaAngle(temp.x, 0)替换temp.x
    • 我设法修复它并发布了代码。非常感谢您的帮助和对我的耐心。
    【解决方案2】:

    我在这里修好了。不确定它是如何工作的,但它确实有效。归功于视频:https://www.youtube.com/watch?v=F5eE1YL1ZJY

    使用 UnityEngine;

    公共类 FPSController : MonoBehaviour {

    public float speed = 5f;
    public float sensitivity = 2f;
    public GameObject Camera;
    
    CharacterController controller;
    
    float moveFB;
    float moveLR;
    
    public float rotX;
    public float rotY;
    
    public float minAngle = -90f;
    public float maxAngle = 90f;
    
    
    
    
    
    void Start()
    {
        controller = GetComponent<CharacterController>();
        Cursor.lockState = CursorLockMode.Locked;
    }
    // Update is called once per frame
    void FixedUpdate ()
    {
        moveFB = Input.GetAxis("Vertical");
        moveLR = Input.GetAxis("Horizontal");
    
        rotX = Input.GetAxis("Mouse X") * sensitivity;
        rotY -= Input.GetAxis("Mouse Y") * sensitivity;
        rotY = Mathf.Clamp(rotY, minAngle, maxAngle);
    
    
        transform.Rotate(0, rotX, 0);
        Vector3 movement = new Vector3(moveLR * speed * Time.deltaTime, 0, moveFB * speed * Time.deltaTime);
        controller.Move(transform.rotation * movement);
        Camera.transform.localRotation = Quaternion.Euler(rotY, 0, 0);
    
    
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-03
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      相关资源
      最近更新 更多