本文へスキップ
Unity Tutorial

Rotate a 3D object with a script in Unity

Create your first C# script and spin a 3D object smoothly with Update and Time.deltaTime.

公開日: 2026-05-06#Unity#C##3D#Beginner
+
*
+
Coco, the guide character・喜ぶ
Coco
Game dev learning guide
This English lesson uses the same guided screen structure as the Japanese tutorials: a visual start, character guidance, step cards, code, mistakes, and a final checklist.
+
*
+
Hajime, the learner character・混乱
Hajime
Beginner game engine learner
I want to know what to make first, what to check, and how to tell whether I actually understood it.
Coco, the guide character
Rotate a 3D object with a script in Unity is designed around one visible result. We will keep the first pass focused so the lesson turns into practice quickly.
Hajime, the learner character
So I should finish the exact lesson once before changing it?
Coco, the guide character
Yes. Finish the stable version first, then change one variable, setting, or layout detail to make the idea yours.

Lesson Overview

Finish one working result first

1.

Goal

This lesson teaches the smallest useful Unity scripting loop: attach a component, update every frame, and see a visible 3D result immediately.

2.

Time

15-20 min / Beginner. Keep the first pass small, then change one thing on your own.

3.

Result

The object rotates as soon as Play starts.

Before You Start

Check the goal, inputs, and success condition

The lesson is easiest to follow when you know what should happen on screen, which control or editor action triggers it, and what counts as done.

1

This lesson teaches the smallest useful Unity scripting loop: attach a component, update every frame, and see a visible 3D result immediately.

2

Understand the role of a MonoBehaviour script attached to a scene object.

3

The object rotates as soon as Play starts.

1Conclusion first

If you are completely new to Unity scripting, rotating a cube is one of the best first exercises because it links code to a visible result in seconds. You do not have to understand every Unity system at once. What matters is learning the small loop of 'write code, attach the script, press Play, observe the result, and adjust a value.'

This lesson is intentionally simple, but the pattern behind it appears everywhere in game development. Once you understand how Update runs, how serialized fields appear in the Inspector, and why Time.deltaTime matters, you have the foundation needed for movement, animation helpers, camera behavior, and many other gameplay scripts.

2What you will learn

  • Understand the role of a MonoBehaviour script attached to a scene object.
  • See how Update runs every frame and why it is useful for continuous motion.
  • Use serialized fields to tune behavior in the Inspector without rewriting code.
  • Build an early habit of frame-rate independent logic with Time.deltaTime.

3Before you start

  • A new Unity project with a simple scene and one Cube object.
  • Basic confidence creating a C# script asset and attaching it to an object.
  • The Inspector and Hierarchy panels visible while you test changes.
  • A willingness to change one value at a time and observe the result carefully.

4Step by step

1
Step 1
Create a cube and a script
Start with a simple Cube in a new scene, then create a C# script such as RotateObject and attach it to the Cube.
2
Step 2
Use Update for frame-by-frame motion
Inside Update, call transform.Rotate with a speed multiplied by Time.deltaTime so the motion stays stable across different frame rates.
3
Step 3
Adjust speed and axis
Expose a speed field and experiment with X, Y, or Z rotation so you can understand local axes and script parameters.

5Sample script

Sample script
csharp
using UnityEngine;

public class RotateObject : MonoBehaviour
{
    [SerializeField] private Vector3 axis = new Vector3(0f, 1f, 0f);
    [SerializeField] private float speed = 90f;

    private void Update()
    {
        transform.Rotate(axis * speed * Time.deltaTime);
    }
}
  • The axis field is exposed so beginners can test X, Y, and Z rotation without touching the code again.
  • The speed value is expressed in degrees per second, which makes the number easier to reason about.
  • Time.deltaTime converts the rotation from 'per frame' into 'per second,' which is why the motion stays more consistent.

6Common mistakes

The object does not rotate at all

This usually means the script was created but never attached to the Cube, or the Console shows a compile error in another script. In Unity, one broken script can stop all scripts from compiling, so always check the Console when something feels strangely inactive.

Rotation looks too fast or too slow

Beginners often forget that 90 means 90 degrees every second, not every frame. If the result feels extreme, lower the speed value first before changing the axis or rewriting the logic.

The wrong axis is rotating

A local object's orientation may already be turned in the scene, so rotating around Y might not visually match your expectation. Try one axis at a time and remember that local orientation affects the result.

7Checklist

  • The object rotates as soon as Play starts.
  • Changing speed in the Inspector changes the motion.
  • You understand why Time.deltaTime keeps movement consistent.

8Practice ideas

  • Create three cubes and give each one a different axis and speed.
  • Pause rotation when the player presses the space bar.
  • Change the script so the object speeds up gradually instead of rotating at a fixed value.

9FAQ

Why start with rotation instead of movement?

Rotation keeps the example focused. You can confirm success instantly without worrying about collisions, floor height, gravity, or camera framing. That makes it a very efficient first scripting lesson.

Should I use FixedUpdate instead of Update?

For this simple visual rotation example, Update is the clearer starting choice. FixedUpdate becomes more important when you are working with Rigidbody-based physics and want to synchronize with the physics step.

What does SerializeField do here?

SerializeField lets a private field still appear in the Inspector. That means you can keep the code tidy while exposing the parts a learner should tweak during testing.

Can I rotate in world space instead of local space?

Yes. Unity's Rotate methods support different spaces. For a beginner lesson, local rotation is usually easier to understand first, then world-space rotation can be added as an experiment.

10Wrap-up

  • A tiny visible script is enough to teach the core Unity loop of attach, play, observe, and refine.
  • Time.deltaTime is not just a detail; it is one of the earliest habits that keeps future code stable.
  • Once this pattern feels comfortable, you are ready to move from rotating an object to moving or reacting to input.

11Character review

Hajime, the learner character
If I can make the result once and explain what changed, I can move on with more confidence.
Coco, the guide character
Exactly. The best next step is one small variation: adjust a value, swap an input, or connect the idea to a tiny scene of your own.
Tiny variation: finish the lesson once exactly as written, then change one value, label, axis, or UI detail. That small change is where understanding starts to stick.

Next tutorials