# Clamping Joystick Throttle to a 0-1 Range

It’s 3am here but thats no excuse, I must be a complete idiot…

My joystick is reporting Axis 4 (Throttle control) as -1 to 1.
So I need to figure out how to clamp that to 0 - 1 so that I can use it as a real variable for speed because obviously it wouldn’t make sense to use only HALF of the throttle controller. Right?

``````float throttle = 0f;

void Update()
{
throttle = Input.GetAxis("Throttle"); // -1 to 1
var clamped = Mathf.Clamp01(throttle);  //Some magic happening in here?
controller.speed = clamped * 10f; //Multiply by 10f so it feels more accurate
}
``````

What on earth am I supposed to be doing?

If you have something in a -1 to 1 range and you want that entire range mapped to 0 to 1 is very straightforward. Divide the value by 2 (so its now -.5 to .5) and then add .5 to it (now its 0 to 1).

Thanks so much! Time to go to sleep, clearly the grey matter is worn out.

``````        throttle = (Input.GetAxis("Mouse ScrollWheel") * 10f);
float speed = (throttle + 1) / 2;
controller.speed = Mathf.Clamp(speed * 10f, 0f, 10f);
``````
1 Like