Module 1: Introduction to Game Development
Module 2: Unity Interface and Basics
Module 3: Introduction to C# Programming for Unity
Module 4: Physics and Movement
Module 5: 2D Game Development
Module 6: 3D Game Development
Module 7: User Interfaces and Game Mechanics
Module 8: Animation and Visual Effects
Module 9: Sound Design and Implementation
Module 10: Building and Deploying Your Game
Module 11: Advanced Topics and Next Steps

3.5 Adding Player Controls

Now let’s allow the player to control the cube using keyboard input.

Step 1: Update the Script
Modify the Update() method:

void Update() {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);
    transform.Translate(movement * Time.deltaTime * 5);
}

Explanation:

  • Input.GetAxis: Captures keyboard input.
    • "Horizontal": Maps to arrow keys ( and ) or A/D.
    • "Vertical": Maps to arrow keys ( and ) or W/S.
  • Vector3: Combines the input into a single direction.

Step 2: Test It

  1. Save the script.
  2. Press Play and use the arrow keys or WASD to move the cube.