Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Mouse sensitivity?

Discussion in 'Scripting' started by Zante, Jan 21, 2010.

  1. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    423
    I'm in the process of creating a small options screen which contains a slider which I want to directly affect the sensitivity of the FPS controller. Since I have multiple cameras, and they all have mouse look scripts, is there a way to affect the core input settings of the program so I don't have to go around referencing each camera/mouselook script manually?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,288
    This is something where it would be appropriate to set a static variable, and then have the mouselook script multiply input by that static variable.

    --Eric
     
  3. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    423
    I just realised that the mouselook script is written in C# and my scripts are all in js.

    I'm having trouble passing variables between the two. Is there an example I can follow somewhere?
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you can't pass variables between C# and JS, you can only access in one direction, given one of the two is in an earlier buildtstep (see the advanced topics in the manual)
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,288
    I re-wrote MouseLook in js a while ago for that reason:

    Code (csharp):
    1. @script AddComponentMenu ("Camera-Control/Mouse Look")
    2. enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    3. var axes = RotationAxes.MouseXAndY;
    4. var sensitivityX : float = 15;
    5. var sensitivityY : float = 15;
    6.  
    7. var minimumX : float = -360;
    8. var maximumX : float = 360;
    9.  
    10. var minimumY : float = -60;
    11. var maximumY : float = 60;
    12.  
    13. var rotationX : float = 0;
    14. var rotationY : float = 0;
    15.  
    16. private var originalRotation : Quaternion;
    17.  
    18. function Update () {
    19.     if (axes == RotationAxes.MouseXAndY) {
    20.         rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    21.         rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    22.  
    23.         rotationX = ClampAngle (rotationX, minimumX, maximumX);
    24.         rotationY = ClampAngle (rotationY, minimumY, maximumY);
    25.        
    26.         var xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
    27.         var yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
    28.        
    29.         transform.localRotation = originalRotation * xQuaternion * yQuaternion;
    30.     }
    31.     else if (axes == RotationAxes.MouseX) {
    32.         rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    33.         rotationX = ClampAngle (rotationX, minimumX, maximumX);
    34.  
    35.         xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
    36.         transform.localRotation = originalRotation * xQuaternion;
    37.     }
    38.     else {
    39.         rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    40.         rotationY = ClampAngle (rotationY, minimumY, maximumY);
    41.  
    42.         yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
    43.         transform.localRotation = originalRotation * yQuaternion;
    44.     }
    45. }
    46.  
    47. function Start () {
    48.     if (rigidbody)
    49.         rigidbody.freezeRotation = true;
    50.     originalRotation = transform.localRotation;
    51. }
    52.  
    53. static function ClampAngle (angle : float, min : float, max : float) : float {
    54.     if (angle < -360.0)
    55.         angle += 360.0;
    56.     if (angle > 360.0)
    57.         angle -= 360.0;
    58.     return Mathf.Clamp (angle, min, max);
    59. }
    --Eric
     
  6. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    423
    Eric, thanks loads for sharing. Haven't tried it yet but cheers in advance.

    Edit: Implemented and works perfectly.
     
  7. adrennalinpro

    adrennalinpro

    Joined:
    Apr 21, 2013
    Posts:
    3
    Thank you eric!
     
unityunity