🏃 Lesson 3.3: Moving Objects with Code
Let's bring an object to life. In this lesson you'll make a cube spin and glide using code — and learn the single most important trick for smooth, fair movement: Time.deltaTime.
🎯 Learning Objectives
- Move an object with
transform.Translate - Rotate an object with
transform.Rotate - Use Vector3 to describe directions
- Understand why
Time.deltaTimekeeps motion smooth and frame-rate independent
Estimated Time: 40 minutes
In This Lesson
Our Target Object
We'll script a simple cube — the humble object from your first scene. It can't show motion in a still image, of course, but pressing Play will make it come alive:
Rotating with Code
Every script can reach its own GameObject's Transform via the word transform. To spin the object a little every frame, put a Rotate call in Update():
using UnityEngine;
public class Spinner : MonoBehaviour
{
void Update()
{
transform.Rotate(0f, 1f, 0f); // turn 1 degree around Y each frame
}
}
Attach this to the cube, press Play, and it spins. But there's a problem hiding in that 1f…
The Time.deltaTime Trick
"1 degree per frame" sounds fine — until you realize different computers run at different frame rates. A fast PC might draw 240 frames per second; a slow one 30. Our cube would spin 8× faster on the fast machine. Unfair and inconsistent!
The fix is Time.deltaTime — the number of seconds since the last frame. Multiply your movement by it and you get a consistent "per second" speed on every machine:
public class Spinner : MonoBehaviour
{
public float speed = 90f; // degrees per SECOND
void Update()
{
transform.Rotate(0f, speed * Time.deltaTime, 0f);
}
}
📖 Definition
Time.deltaTime: the time in seconds that the last frame took. Multiplying movement by it converts "per frame" into "per second," so motion looks the same regardless of frame rate.
💡 Golden rule: any movement or rotation insideUpdate()should be multiplied byTime.deltaTime. Say it with me: "times delta time." You'll write it hundreds of times.
Moving with Translate
To slide an object, use transform.Translate. Directions are given as a Vector3 — three numbers for X, Y, Z. Unity provides handy shortcuts like Vector3.forward and Vector3.up:
public class Glider : MonoBehaviour
{
public float speed = 3f; // units per second
void Update()
{
// move forward (along +Z) at 'speed' units per second
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}
| Shortcut | Direction |
|---|---|
Vector3.forward | +Z (into the screen) |
Vector3.up | +Y (upward) |
Vector3.right | +X (to the right) |
new Vector3(1, 0, 0) | A custom direction (here, +X) |
Hands-on Exercise
🏋️ Exercise: Spin and glide
- Add the
Spinnerscript (withTime.deltaTime) to a cube. Press Play — it spins smoothly. - In the Inspector, change Speed to
180— it now spins twice as fast. Try-90to reverse. - Add a
Gliderscript too, and watch the cube spin and drift forward.
💡 It spins wildly fast / not at all
Wildly fast usually means you forgot * Time.deltaTime. Not at all usually means the script isn't attached or Speed is 0.
✅ Challenge solution: bob up and down
void Update()
{
float y = Mathf.Sin(Time.time * 2f) * 0.5f; // gentle wave
transform.position = new Vector3(0f, 1f + y, 0f);
}
🎯 Quick Quiz
Question 1: Why multiply movement by Time.deltaTime?
Question 2: Which reaches the current object's Transform inside its own script?
Summary
🎉 Key Takeaways
transform.Rotate(...)spins an object;transform.Translate(...)moves it.- Directions use Vector3 (X, Y, Z), with shortcuts like
Vector3.forward. - Always multiply per-frame motion by
Time.deltaTimefor smooth, fair speed. - Expose
speedas a public field to tune motion in the Inspector.
🚀 What's Next?
Code doesn't always work first time — and that's fine. Lesson 3.4 shows you how to read the Console and squash the errors every beginner hits.