- Home /
Anyone have a sensitivity slider script/tutorial used for an fps (using Unity's mouselook controller?)
Ive been trying to figure this out for months, it isnt hard, its just im a beginner and its a giant road block in my game. If anyone has their own FPS game using unity's FPSController and mouselook script please help me it would be very very appreciated. :) Also please tell me how to use it.
Answer by shrey150 · Jul 09, 2017 at 08:27 AM
This post touches on how to do this, Unity made this quite confusing to modify since MouseLook
is private: http://answers.unity3d.com/questions/1095401/unity-5-access-first-person-controller-mouse-look.html
Basically, add this function to FirstPersonController.cs
to allow us to modify the private variables:
public void ChangeMouseSensitivity(float X, float Y){
m_MouseLook.XSensitivity = X;
m_MouseLook.YSensitivity = Y;
}
Then on another script, write a function that takes the slider value and sets that using the previous function. This script should be on the player GameObject, where the FPS script is. Something like this:
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
using UnityEngine.UI;
public class ChangeSettings : MonoBehaviour {
public Slider sensitivitySlider;
public void ApplySensitivity()
{
GetComponent<FirstPersonController>().ChangeMouseSensitivity(sensitivitySlider.value, sensitivitySlider.value);
}
}
Now in the editor, set sensitivitySlider
to the one you want to use and add ApplySensitivity()
as an OnClick
event on the button used to apply the sensitivity. Hope this helped!
EDIT: You can make this even easier. Under the OnValueChanged
event on the slider, add ApplySensitivity()
. That way you can try the sensitivity in real-time.
Im stuck, I did the first steps, but Im confused on how I set sensitivitySlider to the slider I want when the slider I want is on a different scene? And in the editor how do I add ApplySensitivity() as an OnClick event on the button used to apply the sensitivity? Other then that it looks good, please help, Thank you so much!
In the editor, click on the Player GameObject from the hierarchy. Under the FirstPersonController component, there should be a new empty slot called "Sensitivity Slider." Drag the slider you want to use into that location. (If you don't see it, check that ApplySensitivity() on the ChangeSettings script is public, I added that as an edit later). Now if you want to do the button approach where you apply once you're ready, create a button and scroll down until you see "On Click ()" on the Button component. Click the + in the bottom right of the panel, and drag the Player GameObject into the new slot that appears. The "No Function" dropdown will then be enabled. Open that and look for ChangeSettings > ApplySensitivity(). However, if you want to do the realtime approach I mentioned where the sensitivity changes as the slider changes, look for the "On Value Changed ()" panel on the slider ins$$anonymous$$d and repeat the process for adding ApplySensitivity() from there. Contact me if you have any other questions!
I copied your script and put it on my player, also applied your edit to first person controller to allow us to modify the private stuff, but in the Sensitivityslider slot (in the inspector) I cannot add my slider I want to use because it is on a different scene, I cannot drag it onto the slot because of it being in a different scene. And once that is resolved, how do I do the next step? Do I (on the main menu/options scene where the script is) create a on value change (use the + sign etc) and drag the ChangeSettings script you made onto that slot and then choose ApplySensitivity() ? Thats what I'm stuck on, and thank you so much for the help it means a ton!
Your answer
Follow this Question
Related Questions
sensitivity slider/ drop down help 0 Answers
Get a float from a different GameObject? (MouseLook) 1 Answer
No Y-Axis rotation FPS Controller 2 Answers
Sensitivity Slider Issue 1 Answer
Sensitivity sliders not working 1 Answer