Unity Fundamentals: Movement, Physics, and Collisions
In this three-part series, you’ll learn the fundamentals of 2D game development in Unity. By the end of Part 1, you’ll have created a simple car game with movement controls, physics interactions, and a dynamic camera system.
By completing Part 1, you will:
Time.deltaTime
Window → Layouts → Tall
Pro Tip
Save your custom layout by going to Window → Layouts → Save Layout
. Name it “2D Development” for easy access in future projects.
In the Hierarchy panel, right-click in empty space
Navigate to 2D Object → Sprites → Capsule
A capsule sprite will appear in your scene
Rename the GameObject:
Select the Car GameObject in the Hierarchy
In the Inspector, reset the Transform:
Your car should now be at position (0, 0, 0)
Visual Customization
You can change the car’s color by:
The Transform component is fundamental to all GameObjects in Unity. Let’s explore its three main properties:
Try these experiments with your Car selected:
Why the New Input System?
The new Input System provides better performance, flexibility, and support for multiple input devices compared to the legacy system. It is recommended for all new projects. While both are supported, we will use the new Input System in this workshop and in the class. Setting the project to support both systems ensures compatibility with older assets.
Open Project Settings:
Navigate to Player:
Under Configuration:
Unity will prompt to restart - click “Yes”
Create → MonoBehaviour Script
Double-click the Cruise script to open it in your code editor. Replace the default code with:
New Input System
We’re using Unity’s new Input System (UnityEngine.InputSystem
) which provides better performance and more flexibility than the legacy input system. It also allows us to easily support multiple input devices.
Update your Cruise script with these movement variables:
Understanding [SerializeField]
[SerializeField]
makes private variables visible in the Unity Inspector, allowing you to:
Our Variables:
moveSpeed
: Controls how fast the car moves forward/backward (units per frame, will be adjusted with deltaTime)steerSpeed
: Controls how fast the car rotates (degrees per frame, will be adjusted with deltaTime)Update your Cruise script with the movement code:
using UnityEngine;
using UnityEngine.InputSystem;
public class Cruise : MonoBehaviour
{
[SerializeField] float moveSpeed = 1f;
[SerializeField] float steerSpeed = 5f;
void Update()
{
float move = 0f;
float steer = 0f;
// Forward movement (W or Up Arrow)
if (Keyboard.current.wKey.isPressed || Keyboard.current.upArrowKey.isPressed)
{
move = 1f;
}
// Backward movement (S or Down Arrow)
else if (Keyboard.current.sKey.isPressed || Keyboard.current.downArrowKey.isPressed)
{
move = -1f;
}
// Left steering (A or Left Arrow)
if (Keyboard.current.aKey.isPressed || Keyboard.current.leftArrowKey.isPressed)
{
steer = 1f;
}
// Right steering (D or Right Arrow)
else if (Keyboard.current.dKey.isPressed || Keyboard.current.rightArrowKey.isPressed)
{
steer = -1f;
}
// Apply movement and rotation (without deltaTime for now)
transform.Translate(0, move * moveSpeed, 0);
transform.Rotate(0, 0, steer * steerSpeed);
}
}
Understanding the Input System Code:
Keyboard.current
: References the current keyboard device.isPressed
: Returns true while the key is held downSave your script (Ctrl/Cmd + S)
Return to Unity and wait for compilation
Press Play button to enter Play Mode
Test both control schemes:
Note: Both WASD and arrow keys work, so players can use their preferred control scheme
Movement Speed Issue
You’ll notice the car moves VERY fast! This is because movement is happening every frame (30-120 times per second). We’ll fix this in the next step with Time.deltaTime.
Without Time.deltaTime
:
Time.deltaTime
represents the time in seconds since the last frame:
Update your Cruise script to include Time.deltaTime. This will need to be adjusted later when we change speed values:
using UnityEngine;
using UnityEngine.InputSystem;
public class Cruise : MonoBehaviour
{
[SerializeField] float moveSpeed = 5f;
[SerializeField] float steerSpeed = 100f;
void Update()
{
float move = 0f;
float steer = 0f;
if (Keyboard.current.wKey.isPressed || Keyboard.current.upArrowKey.isPressed)
{
move = 1f;
}
else if (Keyboard.current.sKey.isPressed || Keyboard.current.downArrowKey.isPressed)
{
move = -1f;
}
if (Keyboard.current.aKey.isPressed || Keyboard.current.leftArrowKey.isPressed)
{
steer = 1f;
}
else if (Keyboard.current.dKey.isPressed || Keyboard.current.rightArrowKey.isPressed)
{
steer = -1f;
}
// Calculate frame-independent movement
float moveAmount = move * moveSpeed * Time.deltaTime;
float steerAmount = steer * steerSpeed * Time.deltaTime;
// Apply movement and rotation
transform.Translate(0, moveAmount, 0);
transform.Rotate(0, 0, steerAmount);
}
}
With Time.deltaTime
, you’ll need to increase your speed values:
Select your Car in the Hierarchy
In the Inspector, find the Cruise component
Set new values:
Testing Frame Independence
To verify frame independence is working: