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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question on default mouselook?

Discussion in 'Scripting' started by NzRoFL, Jun 9, 2011.

  1. NzRoFL

    NzRoFL

    Joined:
    May 21, 2011
    Posts:
    7
    Ok so with the default mouselook script if I disable it, rotate my player then enable it it reverts the rotation back.

    Any ideas why? I think it might be because of the freezerotation?

    mouselook script:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5.  
    6.  
    7. /// MouseLook rotates the transform based on the mouse delta.
    8.  
    9. /// Minimum and Maximum values can be used to constrain the possible rotation
    10.  
    11.  
    12.  
    13. /// To make an FPS style character:
    14.  
    15. /// - Create a capsule.
    16.  
    17. /// - Add a rigid body to the capsule
    18.  
    19. /// - Add the MouseLook script to the capsule.
    20.  
    21. ///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
    22.  
    23. /// - Add FPSWalker script to the capsule
    24.  
    25.  
    26.  
    27. /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
    28.  
    29. /// - Add a MouseLook script to the camera.
    30.  
    31. ///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
    32.  
    33. [AddComponentMenu("Camera-Control/Mouse Look")]
    34.  
    35. public class MouseLook : MonoBehaviour {
    36.  
    37.  
    38.  
    39.     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    40.  
    41.     public RotationAxes axes = RotationAxes.MouseXAndY;
    42.  
    43.     public float sensitivityX = 15F;
    44.  
    45.     public float sensitivityY = 15F;
    46.  
    47.  
    48.  
    49.     public float minimumX = -360F;
    50.  
    51.     public float maximumX = 360F;
    52.  
    53.  
    54.  
    55.     public float minimumY = -60F;
    56.  
    57.     public float maximumY = 60F;
    58.  
    59.  
    60.  
    61.     float rotationX = 0F;
    62.  
    63.     float rotationY = 0F;
    64.  
    65.  
    66.  
    67.     Quaternion originalRotation;
    68.  
    69.  
    70.  
    71.     void Update ()
    72.  
    73.     {
    74.  
    75.         if (axes == RotationAxes.MouseXAndY)
    76.  
    77.         {
    78.  
    79.             // Read the mouse input axis
    80.  
    81.             rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    82.  
    83.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    84.  
    85.  
    86.  
    87.             rotationX = ClampAngle (rotationX, minimumX, maximumX);
    88.  
    89.             rotationY = ClampAngle (rotationY, minimumY, maximumY);
    90.  
    91.  
    92.  
    93.             Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
    94.  
    95.             Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, -Vector3.right);
    96.  
    97.  
    98.  
    99.             transform.localRotation = originalRotation * xQuaternion * yQuaternion;
    100.  
    101.         }
    102.  
    103.         else if (axes == RotationAxes.MouseX)
    104.  
    105.         {
    106.  
    107.             rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    108.  
    109.             rotationX = ClampAngle (rotationX, minimumX, maximumX);
    110.  
    111.  
    112.  
    113.             Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
    114.  
    115.             transform.localRotation = originalRotation * xQuaternion;
    116.  
    117.         }
    118.  
    119.         else
    120.  
    121.         {
    122.  
    123.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    124.  
    125.             rotationY = ClampAngle (rotationY, minimumY, maximumY);
    126.  
    127.  
    128.  
    129.             Quaternion yQuaternion = Quaternion.AngleAxis (-rotationY, Vector3.right);
    130.  
    131.             transform.localRotation = originalRotation * yQuaternion;
    132.  
    133.         }
    134.  
    135.     }
    136.  
    137.  
    138.  
    139.     void Awake ()
    140.  
    141.     {
    142.  
    143.         // Make the rigid body not change rotation
    144.  
    145.         if (rigidbody)
    146.  
    147.             rigidbody.freezeRotation = true;
    148.  
    149.         originalRotation = transform.localRotation;
    150.  
    151.     }
    152.  
    153.  
    154.  
    155.     public static float ClampAngle (float angle, float min, float max)
    156.  
    157.     {
    158.  
    159.         if (angle < -360F)
    160.  
    161.             angle += 360F;
    162.  
    163.         if (angle > 360F)
    164.  
    165.             angle -= 360F;
    166.  
    167.         return Mathf.Clamp (angle, min, max);
    168.  
    169.     }
    170.  
    171. }
     
  2. geetoo

    geetoo

    Joined:
    Apr 21, 2009
    Posts:
    42
    I think it comes from the Awake function. This is called when you start the game, even if your script is disabled. So the originalQuaternion variable stores the initial transform rotation. One solution would be to rename this Awake function by a Start function. Then, the code would be executed only when your script is enabled.
     
  3. NzRoFL

    NzRoFL

    Joined:
    May 21, 2011
    Posts:
    7
    Hey geetoo,

    thanks for the reply, I tried start instead of awake but it didn't seem to make a difference, someone also mentioned setting the originalRotation, which I tried by setting the var as a static var and using:
    MouseLook.originalRotation = transform.localRotation;
    but that didn't seem to work either?
    Any ideas?

    Thanks
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    You will need to create an OnEnable function. In that pick up the current x and y and apply them.

    http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnEnable.html

    I am not sure if the rotationX and rotationY are simply localEulerAngles. This whole thing would have been simpler if they simply used the MouseOrbit and limited the X as well as the Y.

    To test, do an OnDisable function as well and just have it log the rotationX and rotationY variables out. It may be just as simple as using the localEulerAngles.
     
  5. NzRoFL

    NzRoFL

    Joined:
    May 21, 2011
    Posts:
    7
    Brilliant, I've got it working! Thanks BigMisterB