Hey all,
I am thinking this is probably fairly simple but I can’t put my finger on where I am going wrong. I am trying to use an Input Axis with the positive and negative keys to increase or decrease my player speed by an increment of 20 while keeping a whole number. For example: if player hits W once the speed goes to 20, hits it twice the speed goes to 40, while if the player then hits S the speed drops back to 20, etc.
I am trying to keep with using the Inputs instead of hard coding in W and S keys which is where I am having the trouble. Before hand I was using the Mouse Scroll Wheel with the below code and just set the sensitivity of the mouse scroll input to the increment but it appears the keyboard Axis is different.
Speed += Mathf.RoundToInt(Input.GetAxis("Speed"));
Speed = Mathf.Clamp(Speed, 0, MaxSpeed);
So far I have tried things such as;
if (Input.GetButtonDown("Speed") > 0)
{
Speed = Speed + speedIncrement;
}
else if (Input.GetButtonDown("Speed") < 0)
{
Speed = Speed - speedIncrement;
}
But it didn’t want to do anything. Can anyone point me in the right direction and where I am going wrong?
Would it not be as simple as;
if (Input.GetButtonDown("Speed") > 0)
{
Speed += 20;
}
else if (Input.GetButtonDown("Speed") < 0)
{
Speed -= 20;
}
That’s what I thought? But trying a few variations of it I couldn’t get it to work correctly. It was a Monday evening though so perhaps I was just doing something stupid. I’ll give it another go tonight as long as I know I am going in the right direction with how to handle the Axis.
I am just trying to go for a Rebel Galaxy type speed gauge which should be a really simple task.
Can’t imagine those code snippets would work, since Input.GetButtonDown returns a bool…

If you want to get it all the time, you can use Axis or AxisRaw to check the value.
I also tried to think of a few ways you could get it only for down/up.
Tested only with Up, but might work with Down, too
if (Input.GetButtonUp("Vertical") && Input.GetAxis("Vertical") > 0) print("Up.");
That gave me just 1 output/print statement, so that seemed okay.
1 Like
Your correct, I got the things I tried mixed up in my early morning pre-work weariness 
When I tried the Input.GetAxis > 0 or Input.GetAxisRaw > 0, I would hit the button and it would keep going up when I only want to increase the speed once per button hit but not increase it if holding the button down. So I moved to trying;
If (Input.GetButtonDown(“Speed”))
But couldn’t figure out how to do both positive and negative parts of the Axis this way.
I never thought about using both GetButtonUp/Down and the GetAxis in the same statement, I may have to give that a shot. Googling this issue last night I also came across using a FSM for this type of thing but that just seemed way overdoing something that should be so simple.