【发布时间】: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 度,使其倒置等
【问题讨论】: