- Home /
Question by
bscohen06 · May 13, 2020 at 08:19 PM ·
scripting problemmovementcamera rotate
only rotate on x axis
I put this code in to make the camera rotate and for some reason, the camera only rotates on the x-axis not the y why is this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camerarotate : MonoBehaviour
{
private float mouseX;
private float mouseY;
// Start is called before the first frame update
void Start()
{
Screen.lockCursor = true;
}
// Update is called once per frame
void Update()
{
mouseX += Input.GetAxisRaw("Mouse X");
mouseY += Input.GetAxisRaw("Mouse Y");
transform.localRotation = Quaternion.Euler(Vector3.right * mouseY);
transform.localRotation = Quaternion.Euler(Vector3.up * mouseX);
}
}
Comment
Best Answer
Answer by darksider2000 · May 13, 2020 at 08:47 PM
Hi,
It's because of these two lines in Update()
transform.localRotation = Quaternion.Euler(Vector3.right * mouseY);
transform.localRotation = Quaternion.Euler(Vector3.up * mouseX);
The second change to localRotation is overwriting the first, and you end up only using the second.
You should make a Vector3/Quaternion with the rotation you want to have and then apply it.
void Update()
{
mouseX += Input.GetAxisRaw("Mouse X");
mouseY += Input.GetAxisRaw("Mouse Y");
Vector3 rotationEuler = new Vector3(mouseY, mouseX, 0f);
transform.localRotation = Quaternion.Euler(rotationEuler);
}
Alternatively, you can use Transform.Rotate. That one you can call twice in a row without issue.