Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Camera confusion

Discussion in 'Scripting' started by kromblite, Feb 17, 2015.

  1. kromblite

    kromblite

    Joined:
    Mar 19, 2014
    Posts:
    70
    I've been attempting to program a 3rd person camera that rotates around a central object. I've done this by attaching the camera to the object and rotating the object itself based on mouse movement. I have a separate script for X rotation and Y rotation, and both scripts are pretty much identical. However, the X rotation of the camera can rotate 360 degrees with no problem, but the Y rotation comes to a shaky halt when pointing 90 degrees up or down. Why does the Y rotation have a limit, and how can I get rid of it?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LookY : MonoBehaviour
    5. {
    6.     [SerializeField]
    7.     float _sensitivity=5.0f;
    8.     float _mouseY = 0.0f;
    9.    
    10.     void Update()
    11.     {
    12.         _mouseY = Input.GetAxis("Mouse Y");
    13.  
    14.         Vector3 rot = transform.localEulerAngles;
    15.  
    16.         rot.x -= _mouseY*_sensitivity;
    17.         transform.localEulerAngles = rot;
    18.     }
    19. }
     
  2. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    One approach is to rotate the camera independent of the object.

    It is often useful to interpolate from one camera orientation to another.

    This is typically done using quaternions - this helps avoid gimbal lock.


    Research quaternion camera for more info.
     
  3. kromblite

    kromblite

    Joined:
    Mar 19, 2014
    Posts:
    70
    Alright, I'll look that up. Thanks!