- Home /
Input.GetAxis not returning to zero after key is released
So I'm using Input.GetAxis to retrieve user input for driving a car. I'm am particularly having issues with my vertical axis. When I debug, my horizontal axis show the same problem, however for some reason it does not show any symptoms in-game. By the way, I am using wasd controls.
Anyway, when I press and hold 'w', the car accelerates forwards, and also accelerates backwards if I press and hold 's'. This is as expected. Now lets say I'm going forward, I then let go of 'w' and the car continues to accelerate. The only way I can get it to stop is if I then hold 's' to force the car in the opposite direction. But then even if I let go of 's' the car continues to decelerate before continuing to accelerate backwards. This just goes on and on in an endless cycle.
I will use vertical as a substitution for Input.GetAxis("Vertical"), just for the sake of readability.
I did some debugging and found that vertical does not return to zero when I release 'w' or 's'. When I press 'w' vertical rises to 1 or a value very close to 1, just as expected. However, when I release 'w', vertical does not return to 0, rather it lowers to a number close to zero, usually between 0.1 and 0.2. The only way to make it lower is to press the opposite direction, which makes vertical -1 before then becoming a value between -.2 and -.1 when i release the button.
Now the max torque on my car is 28552.05, so even if vertical was as low as 0.01, I would still be setting the torque of the car's wheels to 285.52 every physics update.
Another interesting thing I found was that vertical seems to be consciously avoiding being 0. Now obviously that is not possible, but here's what I mean by that. I attempted to fix the problem by modifying my code to compensate and set vertical to zero if Input.GetAxis("Vertical") returned anything with an absolute value less than a constant value threshold. I initially made threshold equal to 2. Then I increased it to 2.5. Each time I increased threshold my debugging results changed. I found that the higher the value of threshold, the higher the lowest value of vertical. What I mean by that is whenever I increased the value of threshold, Input.GetAxis("Vertical") still lowered and approached 0 when I released 'w', but it lowered to a higher value than before and vertical never dropped below threshold. I am confused as to how this specific phenomena is possible, and I am particularly interested in how it occurs.
My expected result from releasing 'w' when the car is moving forwards is for it to stop accelerating and begin to coast and lose speed. I am unsure of why I am not getting this result.
Here is my code. I will remove anything related to the horizontal axis since it should have the same solution as the vertical axis.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DriveController : MonoBehaviour {
public List<AxleInfo> axleInfos; // the information about each individual axle
public float maxMotorTorque; // maximum torque the motor can apply to wheel
public float speedThreshold;
public int stepsBelowThreshold;
public int stepsAboveThreshold;
void Start () {
foreach (AxleInfo axleInfo in axleInfos) {
axleInfo.leftWheel.ConfigureVehicleSubsteps (speedThreshold, stepsBelowThreshold, stepsAboveThreshold);
axleInfo.rightWheel.ConfigureVehicleSubsteps (speedThreshold, stepsBelowThreshold, stepsAboveThreshold);
}
}
void FixedUpdate () {
float vertical = Input.GetAxis("Vertical");
float motor = maxMotorTorque * vertical;
foreach (AxleInfo axleInfo in axleInfos) {
if (axleInfo.motor) {
axleInfo.leftWheel.motorTorque = motor;
axleInfo.rightWheel.motorTorque = motor;
}
}
}
}
[System.Serializable]
public class AxleInfo {
public WheelCollider leftWheel;
public WheelCollider rightWheel;
public bool motor; // is this wheel attached to motor?
}
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
WSAD controls work fine for W and S, but randomly get reversed for A and D 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Problems with walking animation 0 Answers