Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

camera is tumbling

Discussion in 'Scripting' started by polyphobia3d, Nov 26, 2010.

  1. polyphobia3d

    polyphobia3d

    Joined:
    Nov 7, 2010
    Posts:
    13
    i am currently digging into javascript and writing some small scripts, but i'm currently having some problems with a camera script. I read out the MouseX and MouseY and use it to change the cameras local rotation

    Code (csharp):
    1.     if (Input.GetMouseButton(2))
    2.     {
    3.  
    4.     var verticalrotation : float = horizontalSpeed * Input.GetAxis ("Mouse X");
    5.     var horizontalrotation : float = verticalSpeed * Input.GetAxis ("Mouse Y");
    6.    
    7.     transform.Rotate (Vector3.right*horizontalrotation);
    8.     transform.Rotate (Vector3.up*verticalrotation);
    9.    
    10.     }
    and

    Code (csharp):
    1.     if (Input.GetMouseButton(2))
    2.     {
    3.  
    4.     var verticalrotation : float = horizontalSpeed * Input.GetAxis ("Mouse X");
    5.     var horizontalrotation : float = verticalSpeed * Input.GetAxis ("Mouse Y");
    6.    
    7.     transform.Rotate (horizontalrotation,verticalrotation,0);
    8.     }
    9.    
    of course do the same, but when i move my mouse in circles over the table it rotates the camera on its local z axis and creates a tumbling motion around the horizon, but i always want the camera to be upright
     
  2. Nikolay116

    Nikolay116

    Joined:
    Mar 21, 2010
    Posts:
    421
    you need to distinguish between global/local rotations
     
  3. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    To elaborate a bit, cumulative local yaw and pitch (which is what you're doing) can indeed result in perceived roll. If you want typical 'FPS-style' mouse control where the object always remains essentially upright, you'll need to use a different method (such as tracking the yaw and pitch angles yourself and then building the orientation from scratch each update, e.g. by assigning to transform.eulerAngles).