Player Movement and Input Systems
# CHAPTER 6
Player Movement and Input Systems
1. Introduction
A game without interactivity is a movie. The core of game development is reading what the player is doing with their keyboard, mouse, or controller, and translating that into action on the screen. In this chapter, we will master Player Movement and Input Systems. We will learn how to read raw keyboard input, explore the three main ways to move an object in Unity (Transform, Rigidbody, and CharacterController), and write a simple script to make the camera follow the player.2. Learning Objectives
By the end of this chapter, you will be able to:-
Read keyboard and mouse input using Unity's
Inputclass.
-
Move an object manually using
transform.Translate.
-
Understand the critical importance of
Time.deltaTime.
-
Move a physics-based object using
Rigidbody.AddForce.
- Understand the basics of a 3D camera follow script.
3. Reading Player Input
Unity has a built-in class calledInput. It listens to your hardware every single frame.
-
Button Presses:
Input.GetKeyDown(KeyCode.Space)returnstruethe exact frame you press the spacebar.
- Axes (Smooth Input): Instead of checking specifically for the 'W' or 'A' keys, Unity uses "Axes".
-
Input.GetAxis("Horizontal")returns-1if you press A (left) and1if you press D (right).
-
Input.GetAxis("Vertical")returns-1for S (backward) and1for W (forward).
4. Method 1: Transform Translation (Kinematic)
The simplest way to move an object is to manually change its coordinates.What is Time.deltaTime?
If you run Translate in Update, it runs every frame. If someone has a fast PC (144 FPS), the player will run twice as fast as someone on a slow PC (60 FPS). Multiplying by Time.deltaTime normalizes the speed to "meters per second," ensuring the game plays exactly the same on all computers!
5. Method 2: Rigidbody Movement (Physics)
If your player needs to bounce off walls, fall via gravity, or push heavy crates, you must use a Rigidbody. Instead of manually translating the position, you push the Rigidbody with forces.6. Method 3: CharacterController (The Industry Standard)
Moving a Rigidbody feels "floaty" and slippery (like a car). For tight, snappy 3D platforming (like Mario or Master Chief), Unity provides the CharacterController component. It allows manual movement likeTranslate, but automatically stops you from walking through walls and helps you walk up stairs!
7. Camera Follow Basics
If the player runs forward, the camera needs to follow them.- The Bad Way: Making the Camera a "child" of the Player. If the player turns around or rolls, the camera will violently spin and make the player nauseous!
- The Good Way: A C# script on the camera that copies the player's position, but adds an "Offset" so it stays slightly behind and above.
8. Visual Learning: The Input Pipeline
9. Best Practices
-
Use SerializeField for References: When writing the Camera script, don't use
GameObject.Find("Player")in the Update loop—it causes massive lag. Instead, usepublic Transform player;and drag the Player object into the slot in the Inspector before the game starts.
10. Common Mistakes
-
Applying DeltaTime to AddForce: You must multiply
TranslatebyTime.deltaTimeto fix framerate issues. However, if you are usingRigidbody.AddForce()insideFixedUpdate(), do NOT multiply byTime.deltaTime!FixedUpdatealready runs on a perfectly locked timer, so adding DeltaTime to it will cause your math to break.
11. Mini Project: Create a Controllable 3D Player
Objective: Build a scene with a floor, a player, and movement code.-
1.
Create a
3D Object -> Plane(The Floor).
-
2.
Create a
3D Object -> Cube(The Player).
-
3.
Create a C# script named
PlayerMovement.
- 4. Open the script and paste the Transform Translation code from Section 4.
- 5. Save the script and drag it onto the Cube.
-
6.
Create a C# script named
CameraFollowand paste the code from Section 7.
-
7.
Attach
CameraFollowto the Main Camera. Click the Camera, look at the Inspector, and drag the Cube into the empty "Player" slot.
- 8. Press Play and drive your cube around!
12. Practice Exercises
- 1. What built-in Unity variable must you multiply movement by to ensure your game speed is independent of the computer's framerate?
- 2. If you are applying physics forces to a Rigidbody, which specific update loop must you place the code in?
13. MCQs with Answers
A programmer wants to check if the player pressed the "Spacebar" to jump during the current frame. Which Unity Input method should they use?
Why is making the Main Camera a direct child of the Player GameObject in the Hierarchy usually a bad idea for 3D games?
14. Interview Questions
-
Q: Explain the necessity of
Time.deltaTime. What specific gameplay bug occurs if a developer usestransform.Translate(Vector3.forward)insideUpdate()without it?
-
Q: Contrast
transform.TranslatewithRigidbody.AddForce. In what specific game genre or scenario would you choose one over the other?
-
Q: Why is a camera follow script traditionally placed in the
LateUpdate()method rather than the standardUpdate()method?
15. FAQs
Q: I heard Unity has a "New Input System." Should I use that? A: Unity recently released a massive, complex "New Input System" package for advanced cross-platform development. It is very powerful, but heavily over-engineered for beginners. Start with the classicInput class (as taught here) to learn the logic of game movement first.
16. Summary
In Chapter 6, we brought our game to life. We learned how to read player intent usingInput.GetAxis and GetKeyDown. We explored the three distinct methods of movement: Kinematic translation (transform.Translate), Physics forces (Rigidbody.AddForce), and the specialized CharacterController. We protected our game from framerate fluctuations using Time.deltaTime, and we wrote a clean, decoupled LateUpdate script to make our camera follow the action smoothly.