using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
class FirstPersonCamera : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerTransform;
public Transform weaponTransform;
private float xRotation = 0;
void Start()
{
UnityEngine.Cursor.lockState = CursorLockMode.Locked; //Hides the cursor and locks it to the center
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90, 90);
transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
weaponTransform.localRotation = Quaternion.Euler(xRotation, 0, 0);
playerTransform.Rotate(new Vector3(0.0f, mouseX, 0.0f));
//playerTransform.gameObject.GetComponent<Rigidbody>().MoveRotation(Quaternion.Euler(0.0f,mouseX,0.0f));
if (Input.GetKeyDown(KeyCode.Escape))
{
UnityEngine.Cursor.lockState = CursorLockMode.None;
}
}
}
这就是我用于播放器外观(鼠标控制)的东西,并且这种情况一直在发生:
即使我锁定了刚体 X 和 Z 旋转。问题似乎主要出现在同时使用键盘控件和鼠标时。这也是 playerMove 代码:https://codeshare.io/wnzrYj。它不应该做任何旋转。
我试过删除 playerTransform.Rotate,问题似乎消失了。我也尝试过通过刚体旋转游戏对象,但这似乎根本没有旋转身体。