I thought, at first, that the conditional would only evaluate as true if I was pressing
one of my “up” keys on my keyboard. But it turns out that that it will also be
true in some cases even after I’ve stopped pressing an “up” key.
But since I don’t really know what it’s supposed to be telling me, I’m not sure why
that’s the case. The only two guesses I have are:
By holding the “up” key down for a while, did I create a buffer for
the input.GetAxis call? So it evaluates as true even though I’ve taken
my finger off the key because…well, basically because it’s being slow and
hasn’t caught up yet?
Or is the call maybe returning non-zero values based on the
current, actual velocity of my rigidbody? So that it will evaluate as true even when
I’m not pressing the “up” button as long as the rigidbody is still moving?
Are one of those right? Can anyone give me some deeper insight into what that function
is really doing?
First, GetAxis does some smoothing in the background, depending on the settings in the Input manager, so yes, for a brief moment after you let go, there’s still a small amount of ‘up’ being recieved. You can bypass this with GetAxisRaw, or by changing the input settings.
Second (the main reason), you are applying the motion to your rigidbody with forces. Even after you have stopped applying the forces (once you stop pressing button) the rigidbody still has momentum, and will keep flying on in the direction it’s moving in, until friction and drag stops it (think of a hockey puck on ice).
that second reason is why the rigidbody keeps moving, right? but (and I guess this is my question) does
the fact that the rigidbody is moving (due to momentum) cause Input.GetAxis(“Vertical”) to return TRUE? or
even though my rigidbody is moving, should Input.GetAxis return FALSE once I’ve stopped pressing any
keys (and after it’s worked through the smoothing)?
Input.GetAxis(“Vertical”) will not return true once you stop pressing the up key. Or rather it will but only for another frame or so.
What Input.GetAxis does is whenever you press the key or joystick associated with that axis it returns a number between -1 and 1. -1 would be down in this case, 0 would be neutral, and 1 would be up. With GetAxis there is a smoothing calculation applied, so if you press Up or W it will start going towards 1 but it might not get there the first frame, you may get a decimal such as 0.33. And once you let go it will smooth back down to 0 so you will still get a small amount of movement. That’s why he said you should use GetAxisRaw, because it just checks if the key is being pushed at that frame, it can ONLY return -1, 0, or 1 never a decimal point. So as soon as you let go you get 0.
@npsf3000, thanks, I did this as per your suggestion and all is clear; thanks! I was getting very thrown by the
smoothing of GetAxis but it makes sense now, thanks all!