我有一个脚本以第三人称视角移动相机,我让相机与角色一起左右转动,但是我无法使相机上下左右转动角色,相机只是会自动打开!
public float mouseSensitivity;
public bool invertMouse;
public bool autoLockCursor;
public Transform cam;
public Transform RotationX;
float x;
void Awake () {
Cursor.lockState = (autoLockCursor)?CursorLockMode.Locked:CursorLockMode.None;
}
void Update () {
//Rotates the player left and right along with the camera
this.gameObject.transform.Rotate(Input.GetAxis("Mouse Y") * mouseSensitivity * ((invertMouse) ? 1 : -1), Input.GetAxis("Mouse X") * mouseSensitivity * ((invertMouse) ? -1 : 1), 0);
this.gameObject.transform.localEulerAngles = new Vector3(0 , this.gameObject.transform.localEulerAngles.y, 0);
//rotates an example object to get its transformer and uses it to rotate the camera
RotationX.gameObject.transform.Rotate(Input.GetAxis("Mouse Y") * mouseSensitivity * ((invertMouse) ? 1 : -1), Input.GetAxis("Mouse X") * mouseSensitivity * ((invertMouse) ? -1 : 1), 0);
RotationX.gameObject.transform.localEulerAngles = new Vector3(RotationX.gameObject.transform.localEulerAngles.x, RotationX.gameObject.transform.localEulerAngles.y, 0);
x = RotationX.gameObject.transform.localEulerAngles.x;
cam.gameObject.transform.localEulerAngles = new Vector3(x , 0, 0);
if (Cursor.lockState == CursorLockMode.None && Input.GetMouseButtonDown(0))
{
Cursor.lockState = CursorLockMode.Locked;
}
else if (Cursor.lockState == CursorLockMode.Locked && Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
}
}
答案 0 :(得分:0)
我研究了Unity文档并找到了解决方案,创建了一个空对象并将相机添加为他的孩子,在轴上移动后,我只需要该对象!
this.gameObject.transform.Rotate(Input.GetAxis("Mouse Y") * mouseSensitivity * ((invertMouse) ? 1 : -1), Input.GetAxis("Mouse X") * mouseSensitivity * ((invertMouse) ? -1 : 1), 0);
this.gameObject.transform.localEulerAngles = new Vector3(this.gameObject.transform.localEulerAngles.x ,0 , 0);