[英]First person camera rotation tilted Unity3d c#
**``` 我正在制作一个游戏,您可以在其中控制机关枪并试图杀死敌人。 没有真正的播放器,只有一个摄像头。 我需要游戏的第一人称视角,所以我在关卡 1 中添加了一些简单的脚本。如果你输了,关卡会重置。 但是,无论何时运行它,相机旋转都会被触发,即使它设置为 0,0,0。 在你死后重生后,由于某种原因,相机正常,场景被重置。 我还注意到相机有时会倾斜
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CrosshairMove : MonoBehaviour
{
public float sens = 100f;
public Rigidbody rb;
public Transform cam;
float xRotation = 0f;
float yRotation = 0f;
public float sceneused = 0f;
public float currentscene = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void FixedUpdate()
{
float mouseX = Input.GetAxis("Mouse X") * sens * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * sens * Time.deltaTime;
xRotation -= mouseY;
yRotation -= mouseX;
//xRotation -= Mathf.Clamp(xRotation, -90f, 90f);
//transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
cam.Rotate(Vector3.up * mouseX);
cam.Rotate(Vector3.left * mouseY);
}
}
我会:
FixedUpdate
。 对于其他任何事情,请使用正常Update
以上所有内容的建议片段:
public GameObject parentGO; // drag ref to parent go here in the editor
void Update {
float mouseX = Input.GetAxis("Mouse X") * lookAtSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * lookAtSensitivity *
Time.deltaTime;
xRot -= mouseY;
xRot = Mathf.Clamp(xRot, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRot, 0f, 0f);
parentGO.transform.Rotate(Vector3.up * mouseX);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.