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

[Solved] Changing the mouse sensitivity of FPController

Discussion in 'Scripting' started by PandawanFr, Mar 15, 2015.

  1. PandawanFr

    PandawanFr

    Joined:
    Nov 27, 2013
    Posts:
    68
    Hello,

    I just imported a new package that's called "Characters" which was available in the basic packages in Unity 5, but I got into a small problem. I want to change the sensitivity of that character's MouseLook with a slider using the new UI. But my problem is that there is no MouseLook in the character, it's integrated in the script FirstPersonController.cs sa you can see here:



    Now what I want is modify it, I found out that the basic MouseLook script is hidden in one of the Script folder of that Package, but I can't modify a script itself, I need a GameObject with the UI slider to modify it. So my question is: Is there a way to modify the sensitivity of FirstPersonController's MouseLook through a UI Slider?

    Thank you!
     
    Last edited: Mar 15, 2015
  2. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
  3. PandawanFr

    PandawanFr

    Joined:
    Nov 27, 2013
    Posts:
    68
    Yes but as you can see, it requires a GameObject that has this Component directly, while the FirstPersonController.cs script creates an instance of it without adding the Script. As you can see in the picture, the MouseLook is part of FirstPersonController.cs it's not a script of it's own, so I can't access these variables.
     
  4. PandawanFr

    PandawanFr

    Joined:
    Nov 27, 2013
    Posts:
    68
    Ok, I found out by myself, I added a small bit of code in the FirstPersonController.cs script.
    Instead of modifying the value from the slider, I'm now getting the value of the slider and changing the sensitivity by that value.

    Here is what I did:


    Added with all other variables:
    Code (CSharp):
    1. public Slider slider;
    Added in the Update function/method:
    Code (CSharp):
    1. m_MouseLook.XSensitivity = slider.value;
    2. m_MouseLook.YSensitivity = slider.value;
    Then in the editor, I dragged my slider to the Slider field, and voilà!
     
  5. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    okay your right.

    you could create a CusomScript attached to the FPSController with public floats X,Y

    and use Update()

    to access GetComponent<FirstPersonController>().MouseLook.XSenn =X
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You can also avoid assigning that continuously in Update if you make a public method which sets the values and then click onto the slider, add an entry to OnValueChanged, choose the FPSController and the appropriate method.
    Just a small improvement though. xD
     
    tobad likes this.
  7. PandawanFr

    PandawanFr

    Joined:
    Nov 27, 2013
    Posts:
    68
    Yeah, but the way to assign methods to UI objects is a bit harder than 4.6 (weirdly, I can't access any methods that I created), so I'll juts keep it this way, but thank you anyway!
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    It should still be the same, you only need a public method in your FPS controller script.
     
  9. PandawanFr

    PandawanFr

    Joined:
    Nov 27, 2013
    Posts:
    68
    Oh so that's the thing! It needs to be public, ok thanks!
     
  10. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    is it possible to forward the slider value to that SetSensitive function directly in the inspector?

    Code (CSharp):
    1. SetSensitive(float value)
    2. {
    3.    this.x = value;
    4. }
    or is it only possible with the reference to the slider to access its current value?

    Code (CSharp):
    1. SetSensitive()
    2. {
    3.    this.x = slider.value;
    4. }

    Edit: i've tested it. both works. good job unity devs!

    I prefer first setup, it uses less relation between the objects
     
    Last edited: Mar 16, 2015
  11. Michio-Magic

    Michio-Magic

    Joined:
    Dec 25, 2014
    Posts:
    18
    Hi guys.
    I too wanted to have a slider to adjust the Mouse Look XSensitivity. (Took a few hours of research on the net, then experimenting. This post was exactly what I needed.)
    My fix is to first add a line to the (new for Unity5.x) FirstPersonController.cs script.

    Code (CSharp):
    1.        
    2. public void SetSensitive(float value)  // added for XSensitivity
    3.         {
    4.             m_MouseLook.XSensitivity = value;
    5.         }
    6.  
    Then I went to the Slider in the UICanvas in the Heirarchy, went to OnValueChanged (Single), selected my 'Player' GameObject, found the FirstPersonController, selected it to link to the function in that script named 'SetSensitive'.

    note: Make sure you have an EventSystem (Script) in the UICanvas, and that Standalone Input Module is active.

    Now when i move the slider it changes the Mouse Look x sensitivity.

    hope this helps !

    set1.png


    set2.png
     
    Zulnamiyu and Whiteleaf like this.