C# SCRIPTING & PLAYER MOVEMENT

Your complete step-by-step walkthrough — follow at your own pace, tick every box

Course: Game On (9GAMZA) Lesson: T1 — L06 Time: ~75 min Unity: 2022.3.x Outcomes: G5-1, G5-2, G5-4
TOTAL XP 0 / 50 XP
1
Your First Script
~10 min • Create a C# script and understand its structure.
+10 XP

Key Terms

C#
"C-Sharp" — the programming language Unity uses. It tells GameObjects how to behave.
Script
A text file containing C# code. Every script is a Component you attach to a GameObject.
Console
A Unity window that shows messages, warnings, and errors from your code.
Component
A building block you add to a GameObject — scripts, Rigidbody, Colliders are all components.
💡
Analogy: If a GameObject is a car, a Script is the driver. The car exists but does nothing until a driver (script) tells it to move!
1.1
Open your L05 project in Unity. Your scene should have sprites with physics from last lesson.
1.2
In the Project window (bottom), right-click on AssetsCreateC# Script.
1.3
IMMEDIATELY type the name PlayerMovement and press Enter.
CRITICAL: Name it RIGHT AWAY before clicking elsewhere! If you click away, it gets named "NewBehaviourScript" and you'll have problems. NO SPACES in the name!
1.4
Double-click the "PlayerMovement" script to open it in a code editor (Visual Studio or VS Code). Wait for it to load — this can take 30-60 seconds the first time.
1.5
You should see code that looks like this:
using UnityEngine; public class PlayerMovement : MonoBehaviour { // Start is called once before the first execution of Update void Start() { } // Update is called once per frame void Update() { } }
This is the skeleton of every Unity script. Two functions are already there: Start() and Update().
Checkpoint: "I created a C# script called PlayerMovement and it's open in the code editor."
Troubleshooting:
  • Script won't open? Right-click → "Open C# Project". Or try Edit → Preferences → External Tools and set your code editor.
  • Visual Studio is very slow? Be patient. It loads many files the first time. Try VS Code instead if it keeps freezing.
2
Start vs Update
~5 min • Understand when code runs.
+10 XP

Key Terms

Start()
Runs ONCE when you press Play. Use for: setting up starting values.
Update()
Runs EVERY FRAME (60+ times per second). Use for: checking input, moving objects.
Frame
One "tick" of the game clock. Games run at 60 frames per second — Update runs each tick.
Debug.Log()
Prints a message to the Console window. Great for testing if your code works!
2.1
Inside the Start() function, between the curly braces { }, type:
void Start() { Debug.Log("Game Started!"); }
2.2
Save the script (Ctrl+S in the code editor). Go back to Unity.
2.3
Drag the PlayerMovement script from the Project window onto your Player object in the Hierarchy. The Inspector should now show "Player Movement" as a component.
2.4
Open the Console window: Window menu → GeneralConsole. Press Play. You should see "Game Started!" in the Console — it runs ONCE.
2.5
Press Stop. Now add a Debug.Log in Update() too — watch how it runs EVERY frame (floods the Console!). Delete the Debug.Log lines from both functions when you're done testing.
Checkpoint: "I understand Start runs once and Update runs every frame. My script is attached to the Player."
3
Capturing Player Input
~10 min • Detect keyboard presses in code.
+10 XP

Key Terms

Input
Unity's system for detecting keyboard, mouse, and controller input.
GetAxis
Returns a number from -1 to 1. Smooth input for movement.
float
A data type for decimal numbers (e.g. 0.5, -1.0, 3.14). Speed and movement use floats.
3.1
In your Update() function (make sure Debug.Log is deleted), type:
void Update() { float moveInput = Input.GetAxis("Horizontal"); Debug.Log(moveInput); }
This line checks what the player is pressing: A/Left = -1, D/Right = +1, nothing = 0.
3.2
Save (Ctrl+S), go to Unity, open the Console, Press Play.
3.3
Press A or Left Arrow — the Console shows numbers going toward -1. Press D or Right Arrow — numbers go toward +1. Release everything — numbers go back to 0.
GetAxis is smooth! It doesn't snap to -1/1 instantly — it eases in and out. This makes movement feel natural rather than jerky.
3.4
Press Stop. Delete the Debug.Log(moveInput); line (we don't need it anymore).
Checkpoint: "I can see input values in the Console when I press A/D. I understand -1 = left, +1 = right."
4
Moving Your Player — Lab
~25 min • Write the movement code and make your player move!
+10 XP

Key Terms

Translate
A function that moves a GameObject by a specific amount each frame.
Time.deltaTime
The time between frames. Multiplying by this makes movement the same speed on ALL computers.
speed
A number that controls how fast the player moves. Higher = faster.
4.1
Replace the code in your Update() function with:
void Update() { float moveInput = Input.GetAxis("Horizontal"); float speed = 5.0f; transform.Translate(moveInput * speed * Time.deltaTime, 0, 0); }
What this does: Each frame, it checks the A/D keys (moveInput), multiplies by speed and deltaTime, then moves the object on the X axis (left/right). The 0,0 means don't move on Y or Z.
4.2
Save (Ctrl+S) and go back to Unity. Make sure the PlayerMovement script is attached to your Player (check the Inspector).
4.3
Press Play! Press A and D — your player should move left and right!
4.4
Experiment with speed: Stop Play mode. Change 5.0f to 10.0f — faster! Try 1.0f — slower! Try 50.0f — zoom! Find a speed that feels good.
4.5
Why Time.deltaTime? Without it, movement would be tied to frame rate. A fast computer running at 120fps would move the player twice as fast as a computer at 60fps! Time.deltaTime fixes this — it makes movement smooth and consistent everywhere.
4.6
Save your scene! Press Ctrl+S in Unity. Your complete script should look like this:
using UnityEngine; public class PlayerMovement : MonoBehaviour { void Start() { } void Update() { float moveInput = Input.GetAxis("Horizontal"); float speed = 5.0f; transform.Translate(moveInput * speed * Time.deltaTime, 0, 0); } }
What you should see
Player moves left and right when pressing A/D. Inspector shows PlayerMovement script component attached.
Checkpoint: "My player moves left and right with A/D keys!"
Troubleshooting:
  • Red error in Console? Read the error — usually a typo. Check spelling, semicolons, and matching { } braces.
  • Not moving? Did you drag the script onto the Player? Check Inspector for "Player Movement" component.
  • Case sensitive! update is NOT Update. getaxis is NOT GetAxis. C# cares!
  • Player falls through floor? Make sure ground still has its Collider from L05.
5
Extension Challenges
If time permits • Take your scripting further.
+10 XP

Only if you've finished Sections 1-4.

5.1
Challenge 1: Vertical Movement. Add another axis! Use Input.GetAxis("Vertical") to get W/S input. Add a second Translate line for Y movement. Now you can move in all 4 directions!
5.2
Challenge 2: Flip the Sprite. When moving left, flip the sprite! In Update, after movement code:
// Flip sprite based on direction if (moveInput < 0) transform.localScale = new Vector3(-1, 1, 1); else if (moveInput > 0) transform.localScale = new Vector3(1, 1, 1);
5.3
Challenge 3: Speed Variable. Move float speed = 5.0f; to the TOP of the class (above Start) and make it public float speed = 5.0f;. Now you can change it in the Inspector without editing code! (We'll learn more about this next lesson.)
5.4
Challenge 4: Enemy Script. Create a NEW script called "EnemyMovement". Make the enemy move automatically (without input) by using a fixed direction in Translate.
🎮
Research: Look up "GetKey vs GetKeyDown vs GetKeyUp" in Unity. What's the difference? When would you use each?