Axis input
- Game Development course
- Beginner
- Lesson 44/83
- 4 min
Learn horizontal and vertical axis input with Input.GetAxis.
What is Axis Input?
In Unity, the Input.GetAxis method reads inputs from keyboard keys or gamepad analog sticks as a smoothed decimal number (float). This value typically ranges between -1.0f and +1.0f. When no key is pressed on the keyboard, it produces 0.
float h = Input.GetAxis("Horizontal");
Debug.Log("Horizontal axis: " + h);Horizontal and Vertical Axes
By default, Unity defines two primary axes: "Horizontal" (A/D or Left/Right arrow keys) and "Vertical" (W/S or Up/Down arrow keys). If you want instant values of -1, 0, or 1 without smoothing, you can use Input.GetAxisRaw.
float v = Input.GetAxis("Vertical");
float rawH = Input.GetAxisRaw("Horizontal");Practice Reading Axis Value
Assign the value of Input.GetAxis("Horizontal") to a float yatay variable. Then if yatay > 0, print Debug.Log("Saga hareket");.
Starter code
using System;
public static class Input
{
public static float GetAxis(string axisName) => axisName == "Horizontal" ? 1.0f : 0.0f;
}
public static class Debug
{
public static void Log(object message) => Console.WriteLine(message);
}
public class Program
{
public static void Main()
{
// yatay eksenini oku ve 0'dan buyukse "Saga hareket" yazdir
}
}Sample solution
using System;
public static class Input
{
public static float GetAxis(string axisName) => axisName == "Horizontal" ? 1.0f : 0.0f;
}
public static class Debug
{
public static void Log(object message) => Console.WriteLine(message);
}
public class Program
{
public static void Main()
{
float yatay = Input.GetAxis("Horizontal");
if (yatay > 0)
{
Debug.Log("Saga hareket");
}
}
}Expected output
Saga hareket
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What value does Input.GetAxis("Horizontal") return when no key is pressed on the keyboard?
- -1.0f
- 0.0f Correct answer
- 1.0f
- null
Explanation: When no directional key is pressed, the axis value is in neutral state, which is 0.0f.
2 Read Vertical Axis
float dikey = Input.____("Vertical");
float dikey = Input.GetAxis("Vertical");
Explanation: To read a decimal value from an axis name, the GetAxis method is used.
3 Axis and Value Matching
| Term | Meaning |
|---|---|
Horizontal |
A/D or Left/Right arrows axis |
Vertical |
W/S or Up/Down arrows axis |
Input.GetAxisRaw |
Returns -1, 0 or 1 without smoothing |
0.0f |
Neutral value when no key is pressed |