0
0
Unityframework~30 mins

Controller/gamepad support in Unity - Mini Project: Build & Apply

Choose your learning style9 modes available
Controller/Gamepad Support in Unity
📖 Scenario: You are making a simple Unity game where the player can move a character using a gamepad or controller. You want to read the controller's joystick input to move the character smoothly.
🎯 Goal: Build a Unity script that reads the horizontal and vertical axes from a gamepad's joystick and moves a character accordingly.
📋 What You'll Learn
Create a Vector2 variable to store joystick input
Create a float variable for movement speed
Use Unity's Input.GetAxis method to read 'Horizontal' and 'Vertical' axes
Move the character's position based on joystick input and speed
Print the current joystick input values to the console
💡 Why This Matters
🌍 Real World
Many games use controllers for player input. Reading joystick axes lets players move characters naturally.
💼 Career
Game developers often implement controller support to make games playable on consoles and with gamepads.
Progress0 / 4 steps
1
Create a Vector2 variable for joystick input
Create a Vector2 variable called joystickInput and set it to Vector2.zero.
Unity
Need a hint?

Use Vector2 joystickInput = Vector2.zero; to create a variable that starts with no input.

2
Add a float variable for movement speed
Add a float variable called moveSpeed and set it to 5f.
Unity
Need a hint?

Use float moveSpeed = 5f; to set how fast the character moves.

3
Read joystick input in Update method
Add an Update() method. Inside it, set joystickInput.x to Input.GetAxis("Horizontal") and joystickInput.y to Input.GetAxis("Vertical").
Unity
Need a hint?

Use Input.GetAxis("Horizontal") and Input.GetAxis("Vertical") to read joystick movement.

4
Move the character and print joystick input
Inside Update(), move the character by adding transform.Translate(joystickInput * moveSpeed * Time.deltaTime). Then print the joystick input using Debug.Log(joystickInput).
Unity
Need a hint?

Use transform.Translate() to move the character and Debug.Log() to print the joystick values.