0
0
Unityframework~10 mins

Input axis for smooth movement in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Input axis for smooth movement
Start
Read Input Axis
Calculate Movement Vector
Apply Movement to Object
Update Frame
Back to Read Input Axis
The program reads the input axis value each frame, calculates movement smoothly, applies it to the object, and repeats every frame.
Execution Sample
Unity
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");
Vector3 move = new Vector3(moveX, 0, moveZ);
transform.Translate(move * speed * Time.deltaTime);
This code reads horizontal and vertical input axes, creates a movement vector, and moves the object smoothly based on input.
Execution Table
FrameInput.GetAxis("Horizontal")Input.GetAxis("Vertical")Movement Vector (x,y,z)ActionObject Position Change
10.00.0(0.0, 0, 0.0)No movementNo change
20.50.0(0.5, 0, 0.0)Move right slowlyPosition moves right by 0.5 * speed * Time.deltaTime
31.00.0(1.0, 0, 0.0)Move right fasterPosition moves right by 1.0 * speed * Time.deltaTime
40.01.0(0.0, 0, 1.0)Move forwardPosition moves forward by 1.0 * speed * Time.deltaTime
5-0.50.5(-0.5, 0, 0.5)Move diagonally left-forwardPosition moves diagonally by vector * speed * Time.deltaTime
60.00.0(0.0, 0, 0.0)Stop movementNo change
💡 Loop continues each frame reading input until program stops
Variable Tracker
VariableStartAfter Frame 1After Frame 2After Frame 3After Frame 4After Frame 5After Frame 6
moveX0.00.00.51.00.0-0.50.0
moveZ0.00.00.00.01.00.50.0
move Vector(0,0,0)(0,0,0)(0.5,0,0)(1,0,0)(0,0,1)(-0.5,0,0.5)(0,0,0)
Key Moments - 3 Insights
Why does the movement feel smooth instead of jumping suddenly?
Because Input.GetAxis returns values gradually changing between -1 and 1, not just -1, 0, or 1. This gradual change is shown in the execution_table rows where moveX and moveZ change smoothly.
What happens if you use Input.GetAxisRaw instead of Input.GetAxis?
Input.GetAxisRaw returns only -1, 0, or 1 instantly, causing movement to jump suddenly without smoothing. This contrasts with the smooth gradual values seen in the execution_table.
Why multiply by Time.deltaTime when moving the object?
Multiplying by Time.deltaTime makes movement frame-rate independent, so the object moves the same speed regardless of how fast frames update. This keeps movement smooth and consistent.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the movement vector at Frame 3?
A(0.0, 0, 1.0)
B(0.5, 0, 0.0)
C(1.0, 0, 0.0)
D(-0.5, 0, 0.5)
💡 Hint
Check the 'Movement Vector (x,y,z)' column at Frame 3 in the execution_table.
At which frame does the object move diagonally?
AFrame 2
BFrame 5
CFrame 4
DFrame 6
💡 Hint
Look for movement vectors with both x and z non-zero in the execution_table.
If Input.GetAxis("Horizontal") returned only -1, 0, or 1 instantly, how would the movement vector change?
AIt would jump suddenly between values
BIt would stay at zero always
CIt would change smoothly like in the table
DIt would become negative only
💡 Hint
Refer to key_moments about difference between Input.GetAxis and Input.GetAxisRaw.
Concept Snapshot
Input.GetAxis reads smooth input values between -1 and 1.
Use these values to create a movement vector.
Multiply by speed and Time.deltaTime for smooth, frame-independent movement.
Apply movement vector to transform.Translate each frame.
This creates smooth player movement responding to input gradually.
Full Transcript
This example shows how Unity's Input.GetAxis method reads smooth input values for horizontal and vertical movement. Each frame, the program reads these values, creates a movement vector, and moves the object accordingly. The values change gradually, not instantly, which makes movement smooth. Multiplying by Time.deltaTime ensures movement speed is consistent regardless of frame rate. The execution table traces input values and movement vectors frame by frame, showing how the object's position changes smoothly. Key moments clarify why Input.GetAxis smooths input and why Time.deltaTime is important. The visual quiz tests understanding of these step-by-step changes.