In this lesson, you will learn how to use the Input Manager’s virtual axes to create fluid, frame-rate independent movement for your game objects.
Understanding Virtual Axes
When you capture raw key presses like Input.GetKey(KeyCode.W), you get a binary "on or off" signal. This feels robotic and jerky. To achieve "smooth" movement, we use the Input Manager, which provides "virtual axes."
Think of a virtual axis as a bridge between your hardware and your code. Instead of asking "Is the W key down?", you ask "What is the current value of the Horizontal or Vertical axis?"
The Input Manager translates keyboard inputs into a floating-point value ranging from -1.0 to 1.0. When you press the 'W' key, the value doesn't jump instantly to 1; it ramps up gradually. When you release the key, it fades back to 0. This "smoothing" effect is built-in, saving you from writing complex acceleration math yourself.
Implementing Axis Movement
To use this, go to Edit > Project Settings > Input Manager. You will see axes like "Horizontal" and "Vertical." To capture these in your script, use the Input.GetAxis method:
void Update()
{
// Get the smoothed input value (-1 to 1)
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
// Create a vector based on the input
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
// Apply movement relative to the transform
transform.Translate(movement * Time.deltaTime * speed);
}
Why This is "Frame-Rate Independent"
Notice the use of Time.deltaTime. Because Input.GetAxis returns a value based on how long you've held the key and the settings in the Input Manager, multiplying it by Time.deltaTime ensures your object moves at the same speed regardless of whether your game runs at 30 FPS or 144 FPS. If you didn't use Time.deltaTime, a player with a faster computer would zip across the screen while a player with a slower one would crawl. By combining virtual axes with frame-rate compensation, you ensure a consistent experience for every player.
Key Takeaway: Virtual axes provide smoothed, gradual input values that, when multiplied by Time.deltaTime, ensure your game objects move consistently and naturally across all hardware.