- Home /
Camera Rotates Incorrectly
I have a simple script setup that is attached to my Camera object that simply rotates around the player when there is input from the mouse. But it seems the axis input is incorrect because when I move the mouse up, it actually rotates on the X axis, and then moving left or right moves it up and down the player on the Y axis. I am on OS X/Mac with MagicMouse.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour {
private const float Y_ANGLE_MIN = -50.0f;
private const float Y_ANGLE_MAX = 50.0f;
public Transform lookAt;
public Transform camTransform;
private Camera cam;
private float distance = 2.0f;
private float currentX = 0.00f;
private float currentY = 0.00f;
private float sensitivityX = 8.0f;
private float sensitivityY = 8.0f;
// Use this for initialization
private void Start () {
camTransform = transform;
cam = Camera.main;
}
// Update is called once per frame
private void Update () {
currentX += Input.GetAxis ("Mouse X");
currentY -= Input.GetAxis ("Mouse Y");
currentY = Mathf.Clamp (currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
}
private void LateUpdate() {
Vector3 dir = new Vector3 (0, 0, -distance);
Quaternion rotation = Quaternion.Euler (currentX, currentY, 0);
cam.transform.position = lookAt.position + rotation * dir;
camTransform.LookAt (lookAt.position);
}
}
Okay I found the issue. I had the axis values flipped on line 37 in the rotation assignment. I updated it to: Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
Answer by Patrick2607 · May 10, 2017 at 09:56 PM
Your problem lies with this line: Quaternion rotation = Quaternion.Euler (currentX, currentY, 0);
. You need to swap currentX
and currentY
.
The parameters of Quaternion.Euler are ordered Z, X, Y, not X, Y, Z like in Vector3.
This will solve it:
Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
Docs: Quaternion.Euler
Edit: I just see you already figured it out... Didn't check the comments. Will keep this answer here for people who are also too lazy or forget to check the comments.
Your answer
![](https://koobas.hobune.stream/wayback/20220612121728im_/https://answers.unity.com/themes/thub/images/avi.jpg)