- Home /
How to script "GetAxisUp"?
I have a game with a fixed 2D camera angle, with a car that drives from left to right. I got the car moving with wheel colliders and basic motor script but for the purposes of gameplay I'd like the car to automatically brake whenever the player lets off the gas (A or D keys), A.K.A GetAxis "Horizontal"
I figure the best way to do this is a simple boolean of if the gas is let up, the car is braked and if the car is braked, apply brakeTorque (does this sound right?). But I don't know how to specify something like Input.GetAxisUp ("Horizontal")
Answer by getyour411 · Sep 13, 2013 at 01:59 AM
You can basically wrap that in an IF:
if(!Input.GetAxis("Horizontal")) { // do brake stuff }
That seems the best solution but when I try to apply it to my code I get an error when trying to apply the brakeTorque. "The '!' operator cannot be applied to operand of type 'float'"
void Update () {
EngineRP$$anonymous$$ = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
ShiftGears();
audio.pitch = $$anonymous$$athf.Abs(EngineRP$$anonymous$$ / $$anonymous$$axEngineRP$$anonymous$$) + 1.0f;
if(audio.pitch > 2.0f) {
audio.pitch = 2.0f;
}
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis ("Vertical");
FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis ("Vertical");
FrontLeftWheel.steerAngle = 10 * Input.GetAxis ("Horizontal");
FrontRightWheel.steerAngle = 10 * Input.GetAxis ("Horizontal");
if(!Input.GetAxis ("Vertical")){
WheelCollider.brakeTorque = $$anonymous$$athf.Infinity;
}
}
O right, I think I've seen that before too, try changing it to
if(!Input.GetAxis("Horizontal") =0)
essentially the same thing - if the button is pressed the axis will not be zero
Answer by Eric5h5 · Sep 13, 2013 at 10:19 PM
I don't think you would want a GetAxisUp function, because that would mean applying the brakes once only, when presumably you want them applied continuously as long as the horizontal axis is not used. So:
if (GetAxis("Horizontal") == 0.0) {
// do brake stuff
}
Now I got void Update () {
EngineRP$$anonymous$$ = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
ShiftGears();
audio.pitch = $$anonymous$$athf.Abs(EngineRP$$anonymous$$ / $$anonymous$$axEngineRP$$anonymous$$) + 1.0f;
if(audio.pitch > 2.0f) {
audio.pitch = 2.0f;
}
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis ("Horizontal");
FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis ("Horizontal");
if(Input.GetAxis ("Horizontal") == 0){
FrontLeftWheel.brakeTorque = $$anonymous$$athf.Infinity;
FrontRightWheel.brakeTorque = $$anonymous$$athf.Infinity;
}
}
No errors, but the car won't move at all. Is $$anonymous$$athf.Infinity not the way to go here? I don't want the car to move at all unless an input is pushed, I figured infinite brakeTorque would do that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613114046im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[SOLVED] Input.GetAxis("Horizontal") returns wrong value? -1 Answers
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Achieve framerate independent AddTorque rotaion with mouse drag. 2 Answers
Get axis literal keyboard or mouse value... not possible? 0 Answers
How do I use an Xbox controller axis other than the left thumbstick? 0 Answers