- Home /
Slider movement steps under keyboard control
I'm writing a mobile VR game and want to play with parameters whilst wearing the headset. I've created a canvas with some sliders that can be selected via a connected bluetooth controller to the phone and adjusted with its left and right. The only problem however is that the steps of movement of the slider are far too large for my purposes. The whole spread seems to be about 10 clicks from end to end. Is there any way to set how much a slider changes under keyboard/controller actions?
Answer by lucianv · Mar 24, 2017 at 03:21 PM
@Nodrap You can use a script and place it on the slider to override its OnMove event behaviour. Here's a little script that worked for me. Hope it helps.
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class SliderMoveHandler : MonoBehaviour, IMoveHandler, IEndDragHandler
{
public float step = 0.1f; // the desired step
Slider slider;
float previousSliderValue = 0f;
void Awake()
{
slider = GetComponent<Slider>();
if (slider)
previousSliderValue = slider.value;
}
public void OnMove(AxisEventData eventData)
{
// override the slider value using our previousSliderValue and the desired step
if (eventData.moveDir == MoveDirection.Left)
{
slider.value = previousSliderValue - step;
}
if (eventData.moveDir == MoveDirection.Right)
{
slider.value = previousSliderValue + step;
}
// keep the slider value for future use
previousSliderValue = slider.value;
}
public void OnEndDrag(PointerEventData eventData)
{
// keep the last slider value if the slider was dragged by mouse
previousSliderValue = slider.value;
}
}
This is a great answer. Very simple script and got it working in no time!
I also added IPointerClickHandler interface with OnPointerClick() function to detect if the player just clicks on the slider. OnEndDrag doesn't pickup click events and was only firing if I slide the slider, not clicking on it.
public void OnPointerClick (PointerEventData eventData) {
previousSliderValue = slider.value;
}
Answer by herDev · Mar 23, 2017 at 11:01 AM
Hi,
You can adjust the sliders min and max value's and then set the amount it should change on the input event by changing the sliders value parameter on a specific input event. Here's the rough idea:
int minValue = 0, maxValue = 100, variationAmount = 2;
Slider sliderRef;
void Start () {
sliderRef = this.gameObject.GetComponent<Slider>();
sliderRef.minValue = minValue;
sliderRef.maxValue = maxValue;
}
void Update ()
{
if (Input.GetKeyDown("a"))
{
sliderRef.value -= variationAmount;
}else if (Input.GetKeyDown("d"))
{
sliderRef.value += variationAmount;
}
}
https://docs.unity3d.com/ScriptReference/UI.Slider-value.html
https://docs.unity3d.com/ScriptReference/UI.Slider-minValue.html
https://docs.unity3d.com/ScriptReference/UI.Slider-maxValue.html
Hope that helps!
UPDATE: For some reason this appeared as a new question to me, but I now see it's four months old. Anyhow I'm guessing you found an answer long ago. Best of luck with your project!
Your answer
Follow this Question
Related Questions
GUI Slider sets a value but the value appears to be reset by the end of OnGUI() 1 Answer
How I can change this command to my Keyboard? 1 Answer
Rotation with a slider 2 Answers
Horizontal Slider 1 Answer
How to Center Horizontal Thumb Slider? 0 Answers