# Input.GetAxis( ) question

Hi,

I’m new to Unity and was reading the script on Input.GetAxis( ) in the official documentation and I’m confused.

http://docs.unity3d.com/412/Documentation/ScriptReference/Input.GetAxis.html

The float returned by Input.GetAxis(“Vertical”) is supposed to be between -1 … 1. If you are multiplying your speed by a number that is only a fraction of 1, like for example, 0.5, aren’t you reducing the speed that you set in half? The comment on the script in the link seems to suggest that the object will travel 10 game units / second. I feel like I’m missing something on how Input.GetAxis ( ) works…A shorthand version of the relevant code is below.

public float speed = 10.0F;

Update ()
{
float translation = Input.GetAxis(“Vertical”) * speed;
translation *= Time.deltaTime;
transform.Translate(0, 0, translation);
}

With key presses, you’re going to get a -1, 0, or a 1. So the vertical axis might be set for the W and S keys. If W is pressed, you get a 1. If it’s not pressed, you get a 0. So when it is pressed, the player will move at the specified speed since you multiplied the speed by 1. And when it’s not pressed, you’ll multiply it by 0 and not move in that direction.

The fractional part can be related to joysticks. With a joystick, you can move fully by moving the joystick as far as it goes, or you can move it just a little bit. If you move it just a little bit (multiply by a fraction of 1), you will move more slowly than if you move it all the way.

What I said here may not be entirely correct, the number returned may be adjusted a bit by some settings specified in the input manager, such as gravity. But generally speaking, that’s how you can think about it.

1 Like

But the script you quoted isn’t multiplying by a fraction, it’s multiplying by 10 (and by the elapsed time). So what’s the problem?

Jason: Thanks! That answered my question. The documentation explicitly says “The value will be in the range -1…1 for keyboard and joystick input.” Which is what lead to the confusion.

Antistone: According to the documentation, Input.GetAxis(“Vertical”) returns a float in the interval [-1,1]. I was asking about the speed being multiplied this number. Yes, Input.GetAxis(“Vertical”) is shown in the code above and in the link.

After some further investigation using Debug.Log(Input.GetAxis(“Horizontal”)), it seems that a tap on the arrow key produces a number less than one. The faster you tap the key, the closer you can get the return value to 1. If you hold down the right arrow, it will return 1 after about 5-6 frames. In effect, this is giving your object an acceleration for a short period of time until it hits its max speed, which is the speed that you coded in, in this case, 10 game units / sec. Your object will no longer accelerate when you hit your max speed.

For someone who wants return values of only {-1,0,1}, they can use Input.GetAxisRaw(“Horizontal”). With this command, the movement is a lot sharper when it comes to changing directions. For example, if velocity is at +10, then I hit the left key, my velocity suddenly changes to -10. Therefore InputRawAxis( ) gives a much faster acceleration than InputAxis when changing direction or starting motion. Always. Holding down the key or not.

In conclusion, the documentation was correct when it said that that “The value will be in the range -1…1 for keyboard and joystick input.” Wish they would have explained it better in their documentation.

As I said, the value will be adjusted by settings in the input manager. One of those settings is called gravity - gravity specifies how fast the value will approach 1 upon pressing the key. Playing around with these values in the input manager should get you the result you want.