Search Unity

Camera Off Centre

Discussion in 'Editor & General Support' started by MerlinG2, Aug 5, 2020.

  1. MerlinG2

    MerlinG2

    Joined:
    Jun 17, 2020
    Posts:
    44
    For some odd reason whenever I press the 'play' button to test my scene, the player camera moves a little to the right while the player body stays in the same position. When I rotate my character, everything moves as you would expect only the camera stays off centre.

    I narrowed it down to the script for the camera causing the issue only I don't know how. I was hoping that maybe someone could help? Here is my code for the player camera:

    public class MouseLook : MonoBehaviour
    {

    public float mouseSensitivity = 120f;

    public Transform playerBody;

    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
    Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    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, -90f, 90f);

    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    playerBody.Rotate(Vector3.up * mouseX);
    }
    }

    I am relatively new to Unity so if I'm missing something really obvious, then that is why.

    Thanks!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,908
    What does the camera's transform look like in the inspector?
     
  3. MerlinG2

    MerlinG2

    Joined:
    Jun 17, 2020
    Posts:
    44
    Never mind, I found the issue. At some point I altered the camera's position and it was just resetting when the scene loaded haha! I'm such and idiot XD
     
    PraetorBlue likes this.