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

problem with camera movement

Discussion in 'Scripting' started by eskovas, Dec 13, 2010.

  1. eskovas

    eskovas

    Joined:
    Dec 2, 2009
    Posts:
    1,373
    hi all,
    really need help with this since i've tryied so many things and none worked...
    well what i want to do is rotate the camera only on the XX axis and the body(player) on the YY axis
    the problem is that when it reaches a certain angle it inverts the mouse movement.

    the script is attatch to the camera

    here is the hierarchi of the player:
    Player ----
    camera
    object

    here is the script :

    Code (csharp):
    1.  
    2.  
    3. @script AddComponentMenu ("Camera-Control/Mouse Look JS")
    4. enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    5. var axes = RotationAxes.MouseXAndY;
    6. var sensitivityX : float = 8;
    7. var sensitivityY : float = 8;
    8.  
    9. var minimumX : float = -360;
    10. var maximumX : float = 360;
    11.  
    12. var minimumY : float = -60;
    13. var maximumY : float = 60;
    14.  
    15. var rotationX : float = 0;
    16. var rotationY : float = 0;
    17.  
    18. var Player : GameObject;
    19.  
    20. private var originalRotation : Quaternion;
    21.  
    22. function Update () {
    23.    if (axes == RotationAxes.MouseXAndY) {
    24.       rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    25.       rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    26.  
    27.       rotationX = ClampAngle (rotationX, minimumX, maximumX);
    28.       rotationY = ClampAngle (rotationY, minimumY, maximumY);
    29.        
    30.       var xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
    31.       var yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
    32. >-------------------------
    33.       var Rot = originalRotation * xQuaternion * yQuaternion;
    34.       transform.localRotation.x = Rot.x;
    35.       Player.transform.localRotation.y = Rot.y;
    36. <-------------------------
    37.    }
    38.    else if (axes == RotationAxes.MouseX) {
    39.       rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    40.       rotationX = ClampAngle (rotationX, minimumX, maximumX);
    41.  
    42.       xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
    43.       transform.localRotation = originalRotation * xQuaternion;
    44.    }
    45.    else {
    46.       rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    47.       rotationY = ClampAngle (rotationY, minimumY, maximumY);
    48.  
    49.       yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
    50. >-----------------
    51.       Player.transform.localRotation = originalRotation * yQuaternion;
    52. <-----------------
    53.    }
    54.    // Adjust Aming Speed
    55.     if ( Input.GetButton("Sights")){
    56.         Aiming ();
    57.     }
    58.     else if ( Input.GetButton("Sights")){
    59.         Normal();
    60.     }
    61.    
    62.    
    63. }
    64.  
    65. function Start () {
    66.    if (rigidbody)
    67.       rigidbody.freezeRotation = true;
    68.    originalRotation = transform.localRotation;
    69.  
    70. }
    71.  
    72. static function ClampAngle (angle : float, min : float, max : float) : float {
    73.    if (angle < -360.0)
    74.       angle += 360.0;
    75.    if (angle > 360.0)
    76.       angle -= 360.0;
    77.    return Mathf.Clamp (angle, min, max);
    78. }
    79.  
    80. function Aiming ()
    81. {
    82.     sensitivityX = 1;
    83.     sensitivityY = 1;
    84. }
    85.  
    86. function Normal ()
    87. {
    88.     sensitivityX = 8;
    89.     sensitivityY = 8;
    90. }
    91.  
    92.  
    93.  
    between the >---- <---- is the code that i changed

    well really hope you can help me with this
    thanks in advance
     
  2. eskovas

    eskovas

    Joined:
    Dec 2, 2009
    Posts:
    1,373
    anyone?
     
  3. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    What does 'XX axis' and 'YY axis' mean?
     
  4. eskovas

    eskovas

    Joined:
    Dec 2, 2009
    Posts:
    1,373
    it's the X and Y axis
     
  5. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    I doubt you really want to be modifying the individual elements of the rotation quaternions as you're doing currently (remember, the elements of the 'rotation' and 'localRotation' fields are not angles).
     
  6. eskovas

    eskovas

    Joined:
    Dec 2, 2009
    Posts:
    1,373
    hmm...
    do you have any tips on how to do this?