- Home /
Clamp not working. (solved)
So I have set up a clamp to stop my throttle from going over or under an amount. This works fine but if I hold down my accelerate button the value will go over the amount but not by much, I don't mind this much but its when I brake and my speed goes into the minuses and I start to reverse that I get annoyed. I don't know how to fix this and didn't see anyone else with this problem so help would be really nice.
Post your script! How do you expect to get any help without showing your code?
$$anonymous$$y best guess would be it you are still adding to the variable's value even though it exceeds the maximum clamp value. But as @agoaldonaletto stated. Post your script.
Here is my code.
#pragma strict
private var Throttle : int = 0;
function Update () {
Throttle = $$anonymous$$athf.Clamp(Throttle,0,$$anonymous$$axThrottle);
Throttle +=Input.GetAxis("Throttle");
if(Input.GetAxis("Brake") > 0)
{
Throttle-=(Throttle/10+1);
}
transform.Translate(Vector3.forward*Time.deltaTime*(Throttle/10));
}
Answer by Freaking-Pingo · Nov 04, 2013 at 07:41 PM
Switch the order of the lines:
Throttle = Mathf.Clamp(Throttle,0,MaxThrottle);
Throttle +=Input.GetAxis("Throttle");
to
Throttle +=Input.GetAxis("Throttle");
Throttle = Mathf.Clamp(Throttle,0,MaxThrottle);
If it resolved your problem, remember to mark my reply as the answer :).
Your answer
![](https://koobas.hobune.stream/wayback/20220613121203im_/https://answers.unity.com/themes/thub/images/avi.jpg)