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

Making a mouse sensitivity slider from the RigidBodyFPSController

Discussion in 'Scripting' started by denissuu, Feb 26, 2020.

  1. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162
    Hi there everyone!
    I am using the unity Standard Assets RigidbodyFPSController script for my character, and I can't seem to figure out how to make a sensitivity slider that adjusts the sensitivity from this script!I'm relatively new to Unity, and I can't seem to find the X and Y sensitivity values anywhere in the script, even though you can modify them in the inspector.

    If anyone knows how I could manage to achieve this, I would really appreciate it!
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    In the inspector?

    [Range(0,10)]
     
    denissuu likes this.
  3. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162

    I didn't really understand what you meant here to be honest, can you be a bit more explicit?
     
  4. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    Sorry! If you wanted a slider in the inspector?
    Using Range before you int (or float etc) in a class (not in a function) will result in this.

    [Range(0,1)]
    int number;

    If not, post up the script and point where you can't figure out stuff.
     
  5. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162

    Oh, I guess that I wasn't clear enough.I wanted to do an in-game sensitivity slider via UI that you could use to change the sensitivity.

    Here are the scripts :


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityStandardAssets.Characters.FirstPerson;
    6.  
    7. public class ChangeSens : MonoBehaviour
    8. {
    9.     public Slider sensitivitySlider;
    10.  
    11.     public void ApplySensitivity()
    12.     {
    13.         GetComponent<MouseLook>().ChangeMouseSensitivity(sensitivitySlider.value);
    14.     }
    15. }
    This is set to be on value changed on the slider.

    And this is the mouse look script and what I inserted to try to make it work.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityStandardAssets.CrossPlatformInput;
    4.  
    5. namespace UnityStandardAssets.Characters.FirstPerson
    6. {
    7.     [Serializable]
    8.     public class MouseLook
    9.     {
    10.         public float XSensitivity = 2f;
    11.         public float YSensitivity = 2f;
    12.         public bool clampVerticalRotation = true;
    13.         public float MinimumX = -90F;
    14.         public float MaximumX = 90F;
    15.         public bool smooth;
    16.         public float smoothTime = 5f;
    17.         public bool lockCursor = true;
    18.  
    19.  
    20.         private Quaternion m_CharacterTargetRot;
    21.         private Quaternion m_CameraTargetRot;
    22.         private bool m_cursorIsLocked = true;
    23.  
    24.         [HideInInspector]
    25.         public Vector2 lookAxis;
    26.  
    27.         public void Init(Transform character, Transform camera)
    28.         {
    29.             m_CharacterTargetRot = character.localRotation;
    30.             m_CameraTargetRot = camera.localRotation;
    31.         }
    32.  
    33.  
    34.         public void LookRotation(Transform character, Transform camera)
    35.         {
    36.             float yRot = lookAxis.x * XSensitivity;
    37.             float xRot = lookAxis.y * YSensitivity;
    38.  
    39.             m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
    40.             m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
    41.  
    42.             if(clampVerticalRotation)
    43.                 m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
    44.  
    45.             if(smooth)
    46.             {
    47.                 character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
    48.                     smoothTime * Time.deltaTime);
    49.                 camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
    50.                     smoothTime * Time.deltaTime);
    51.             }
    52.             else
    53.             {
    54.                 character.localRotation = m_CharacterTargetRot;
    55.                 camera.localRotation = m_CameraTargetRot;
    56.             }
    57.  
    58.             UpdateCursorLock();
    59.         }
    60.  
    61.         public void SetCursorLock(bool value)
    62.         {
    63.             lockCursor = value;
    64.             if(!lockCursor)
    65.             {//we force unlock the cursor if the user disable the cursor locking helper
    66.                 Cursor.lockState = CursorLockMode.None;
    67.                 Cursor.visible = true;
    68.             }
    69.         }
    70.  
    71.         public void UpdateCursorLock()
    72.         {
    73.             //if the user set "lockCursor" we check & properly lock the cursos
    74.             if (lockCursor)
    75.                 InternalLockUpdate();
    76.         }
    77.  
    78.         private void InternalLockUpdate()
    79.         {
    80.             if(Input.GetKeyUp(KeyCode.Escape))
    81.             {
    82.                 m_cursorIsLocked = false;
    83.             }
    84.             else if(Input.GetMouseButtonUp(0))
    85.             {
    86.                 m_cursorIsLocked = true;
    87.             }
    88.  
    89.             if (m_cursorIsLocked)
    90.             {
    91.                 Cursor.lockState = CursorLockMode.Locked;
    92.                 Cursor.visible = false;
    93.             }
    94.             else if (!m_cursorIsLocked)
    95.             {
    96.                 Cursor.lockState = CursorLockMode.None;
    97.                 Cursor.visible = true;
    98.             }
    99.         }
    100.  
    101.         Quaternion ClampRotationAroundXAxis(Quaternion q)
    102.         {
    103.             q.x /= q.w;
    104.             q.y /= q.w;
    105.             q.z /= q.w;
    106.             q.w = 1.0f;
    107.  
    108.             float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
    109.  
    110.             angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
    111.  
    112.             q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
    113.  
    114.             return q;
    115.         }
    116.  
    117.         public void ChangeMouseSensitivity(float sens)
    118.         {
    119.             XSensitivity = sens;
    120.             YSensitivity = sens;
    121.         }
    122.     }
    123. }
    124.  
    Currently the slider doesn't change the x and y sensitivity values and I've been stuck here for about 3 days.. :/