Input axis lets your game character move smoothly instead of jumping in steps. It reads how much you press a key or move a joystick.
Input axis for smooth movement in Unity
float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical");
"Horizontal" and "Vertical" are default input axis names in Unity for left/right and up/down controls.
Input.GetAxis returns a float between -1 and 1, showing how much the control is pressed.
float moveX = Input.GetAxis("Horizontal");
float moveY = Input.GetAxis("Vertical");
float moveX = Input.GetAxisRaw("Horizontal");
This script moves a game object smoothly based on keyboard or joystick input. The character moves left/right and forward/back smoothly using Input.GetAxis.
using UnityEngine; public class SmoothMovement : MonoBehaviour { public float speed = 5f; void Update() { float moveX = Input.GetAxis("Horizontal"); float moveZ = Input.GetAxis("Vertical"); Vector3 move = new Vector3(moveX, 0, moveZ) * speed * Time.deltaTime; transform.Translate(move, Space.World); } }
Input.GetAxis smooths input over time, so movement feels natural.
Use Input.GetAxisRaw if you want instant input without smoothing.
Always multiply by Time.deltaTime to keep movement smooth and frame-rate independent.
Input axis reads how much a control is pressed, giving smooth values between -1 and 1.
Use Input.GetAxis for smooth gradual movement in games.
Combine axis input with speed and Time.deltaTime for smooth character movement.
Practice
Input.GetAxis function return when used for movement?Solution
Step 1: Understand Input.GetAxis behavior
Input.GetAxisreturns a float value that smoothly changes between -1 and 1 based on input device movement, like joystick or keyboard keys.Step 2: Compare with other input types
UnlikeInput.GetButtonorInput.GetKey, it does not return boolean or integer but a float for smooth control.Final Answer:
A smooth float value between -1 and 1 representing input strength -> Option DQuick Check:
Input.GetAxis returns float between -1 and 1 [OK]
- Thinking it returns only 0 or 1
- Confusing with boolean input methods
- Expecting integer values instead of float
Solution
Step 1: Check method usage
Input.GetAxisrequires a string parameter with axis name in quotes, e.g., "Horizontal".Step 2: Validate syntax correctness
float move = Input.GetAxis("Horizontal"); uses correct method call with parentheses and string argument. Other options misuse syntax with missing quotes, wrong brackets, or dot notation.Final Answer:
float move = Input.GetAxis("Horizontal"); -> Option CQuick Check:
Use parentheses and quotes for axis name [OK]
- Omitting quotes around axis name
- Using square brackets instead of parentheses
- Calling GetAxis as a property instead of method
float horizontal = Input.GetAxis("Horizontal");
float speed = 5f;
float movement = horizontal * speed * Time.deltaTime;
Debug.Log(Math.Round(movement, 2));Solution
Step 1: Understand Input.GetAxis with right arrow
Pressing right arrow sets horizontal axis near +1 smoothly.Step 2: Calculate movement value
Movement = 1 * 5 * (1/60) ≈ 0.0833, rounded to 0.08.Final Answer:
A positive float value close to 0.08 (assuming 60 FPS) -> Option AQuick Check:
Right arrow -> horizontal ~1 -> movement ~0.08 [OK]
- Assuming Input.GetAxis returns only 0 or 1
- Ignoring Time.deltaTime in movement calculation
- Confusing positive and negative axis values
float move = Input.GetAxis(Horizontal); transform.Translate(move * speed * Time.deltaTime, 0, 0);
Solution
Step 1: Check GetAxis parameter syntax
The axis name must be a string in quotes. Here, Horizontal is used without quotes causing a compile error.Step 2: Verify other code parts
Using Translate with speed and Time.deltaTime is correct. Variable 'speed' is assumed declared elsewhere.Final Answer:
Missing quotes around "Horizontal" in GetAxis -> Option AQuick Check:
Axis names need quotes in GetAxis() [OK]
- Forgetting quotes around axis name
- Confusing Translate with other movement methods
- Assuming Time.deltaTime is optional
float h = Input.GetAxis("Horizontal");
if (h > 0.1f) {
transform.Translate(h * speed * Time.deltaTime, 0, 0);
}
B) float h = Input.GetAxis("Horizontal");
if (Mathf.Abs(h) > 0.1f) {
transform.Translate(h * speed * Time.deltaTime, 0, 0);
}
C) float h = Input.GetAxis("Horizontal");
if (h > 0.1f && h < -0.1f) {
transform.Translate(h * speed * Time.deltaTime, 0, 0);
}
D) float h = Input.GetAxis("Horizontal");
if (h != 0) {
transform.Translate(h * speed * Time.deltaTime, 0, 0);
}Solution
Step 1: Understand input magnitude filtering
We want to ignore small inputs less than 0.1 in magnitude, so absolute value check is needed.Step 2: Analyze each condition
Checks if h > 0.1f only (ignores negative direction) fails to move left since negative h won't exceed 0.1f. Uses Mathf.Abs to check if absolute value of h is greater than 0.1 uses Mathf.Abs(h) > 0.1f, which is concise and correct. Checks if h is both greater than 0.1 and less than -0.1 (impossible)'s AND condition is impossible. Checks if h is not zero, allowing very small movements allows any non-zero input, including tiny values.Final Answer:
Uses Mathf.Abs to check if absolute value of h is greater than 0.1 -> Option BQuick Check:
Use Mathf.Abs(h) > threshold for magnitude check [OK]
- Checking only positive direction (h > 0.1f), ignoring negatives
- Not using absolute value to check magnitude
- Allowing tiny inputs by only checking h != 0
Ready to prove it?
Pass the test and this pattern moves unknown
