0
0
Unityframework~10 mins

Controller/gamepad support in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Controller/gamepad support
Start Game
Initialize Input System
Detect Connected Controllers
Read Controller Input
Map Input to Actions
Update Game State
Render Frame
Back to Read Controller Input
The game starts by initializing the input system, detecting controllers, reading their input, mapping inputs to game actions, updating the game state, and rendering frames in a loop.
Execution Sample
Unity
void Update() {
  float moveX = Input.GetAxis("Horizontal");
  float moveY = Input.GetAxis("Vertical");
  Vector3 move = new Vector3(moveX, 0, moveY);
  transform.Translate(move * speed * Time.deltaTime);
}
This code reads the horizontal and vertical axes from a controller and moves the player accordingly each frame.
Execution Table
FrameInput.GetAxis("Horizontal")Input.GetAxis("Vertical")Move VectorAction Taken
10.00.0(0.0, 0, 0.0)No movement
20.50.0(0.5, 0, 0.0)Move right
30.50.7(0.5, 0, 0.7)Move diagonally forward-right
40.0-1.0(0.0, 0, -1.0)Move backward
50.00.0(0.0, 0, 0.0)No movement
💡 Loop continues every frame reading input and moving player accordingly.
Variable Tracker
VariableStartAfter Frame 1After Frame 2After Frame 3After Frame 4After Frame 5
moveX0.00.00.50.50.00.0
moveY0.00.00.00.7-1.00.0
move(0,0,0)(0,0,0)(0.5,0,0)(0.5,0,0.7)(0,0,-1)(0,0,0)
Key Moments - 3 Insights
Why does moveX sometimes have values between -1 and 1 instead of just -1, 0, or 1?
Input.GetAxis returns a smooth value between -1 and 1 to allow gradual movement, as shown in the execution_table rows 2 and 3 where moveX is 0.5.
What happens if no controller is connected?
Input.GetAxis returns 0, so move vector is zero and the player does not move, as seen in frame 1 and 5 in the execution_table.
Why do we multiply by Time.deltaTime when moving the player?
Multiplying by Time.deltaTime makes movement smooth and frame-rate independent, ensuring consistent speed across frames.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of moveY at Frame 3?
A0.7
B0.0
C-1.0
D0.5
💡 Hint
Check the 'Input.GetAxis("Vertical")' column at Frame 3 in the execution_table.
At which frame does the player move diagonally forward-right?
AFrame 1
BFrame 3
CFrame 2
DFrame 4
💡 Hint
Look at the 'Action Taken' column in the execution_table for the diagonal movement description.
If the controller is disconnected, what would Input.GetAxis return and how would the player move?
AReturns -1, player moves backward
BReturns 1, player moves forward
CReturns 0, player does not move
DReturns random values, player moves erratically
💡 Hint
Refer to the explanation in key_moments about no controller connected and frame 1 and 5 in execution_table.
Concept Snapshot
Unity controller support uses Input.GetAxis to read controller sticks.
Values range from -1 to 1 for smooth movement.
Multiply movement by Time.deltaTime for smooth frame-rate independent motion.
Check for controller connection; no input means zero movement.
Update player position each frame in Update() method.
Full Transcript
This visual execution shows how Unity reads controller input each frame using Input.GetAxis for horizontal and vertical axes. The values range smoothly between -1 and 1, allowing gradual movement. The move vector is calculated from these inputs and multiplied by speed and Time.deltaTime to move the player smoothly and consistently regardless of frame rate. The execution table traces input values and resulting movement actions frame by frame. Key moments clarify why input values are smooth floats, what happens if no controller is connected, and why Time.deltaTime is used. The quiz tests understanding of input values and movement behavior. This helps beginners see how controller input drives player movement in Unity.