Unity :CharacterMovement

Unity Beginner Tutorial: Moving, Rotating, and Scaling Objects

Estimated reading: 3 minutes 19 views Contributors

Unity Beginner Tutorial: Moving, Rotating, and Scaling Objects

What You’ll Learn

  • How to make a GameObject move and rotate
  • How to make it grow and shrink over time
  • How to detect when it hits another object
  • How to write and understand basic C# scripts in Unity

Step 1: Set Up the Scene

  1. Open Unity Hub, create a new 3D project.
  2. In the Hierarchy, right-click → 3D Object → Cube.
    This cube will be your moving object.
  3. Rename it to PlayerCube.
  4. Right-click in the Hierarchy again → 3D Object → Cube, rename this one Target.
    • Move it a few units forward (for example, set its Position Z to 5).

Step 2: Add Colliders and Rigidbody

Colliders make objects detect collisions.
Rigidbody makes them respond to physics.

  1. Select PlayerCube.
    • It already has a Box Collider by default.
    • Click Add Component → search for Rigidbody and add it.
    • Uncheck “Use Gravity” if you don’t want it to fall.
  2. Select Target.
    • It has a Box Collider too, so no Rigidbody needed (it can just stay still).

Step 3: Create the Script

  1. In the Project window, right-click in the Assets folder → Create → C# Script.
  2. Name it SimpleMover.
  3. Double-click it to open it in your code editor (Visual Studio, Rider, or VS Code).

Step 4: Write the Code

Replace everything in the new script with this:

using UnityEngine;

public class SimpleMover : MonoBehaviour
{
    // Movement speed
    public float moveSpeed = 5f;
    
    // Rotation speed
    public float rotationSpeed = 90f;
    
    // Scale speed
    public float scaleSpeed = 1f;
    
    // Maximum and minimum sizes
    public float maxScale = 2f;
    public float minScale = 0.5f;

    // Used to track if object is growing or shrinking
    private bool growing = true;

    void Update()
    {
        // Move forward constantly
        transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

        // Rotate around the Y-axis (spinning effect)
        transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);

        // Scale up and down over time
        Vector3 currentScale = transform.localScale;

        if (growing)
        {
            currentScale += Vector3.one * scaleSpeed * Time.deltaTime;
            if (currentScale.x >= maxScale) growing = false;
        }
        else
        {
            currentScale -= Vector3.one * scaleSpeed * Time.deltaTime;
            if (currentScale.x <= minScale) growing = true;
        }

        transform.localScale = currentScale;
    }

    private void OnCollisionEnter(Collision collision)
    {
        // Check if the object we hit is named "Target"
        if (collision.gameObject.name == "Target")
        {
            Debug.Log("Hit the Target!");
            GetComponent<Renderer>().material.color = Color.red;
        }
    }
}

Step 5: Add the Script to the Object

  1. Drag the SimpleMover script onto PlayerCube in the Hierarchy.
  2. In the Inspector, you’ll see the variables (moveSpeed, rotationSpeed, etc.) appear.
  3. You can adjust these numbers while the game is running to see what they do.

Step 6: Test It!

  1. Hit Play in Unity.
  2. Watch your cube move forward, spin, and grow/shrink over time.
  3. When it collides with the Target cube, check the Console — it should print “Hit the Target!”
  4. The cube’s color will turn red.

Step 7: Experiment!

Once it’s working, play with the code:

  • Try transform.Translate(Vector3.right * moveSpeed * Time.deltaTime) to move sideways.
  • Change Vector3.up in the rotation to Vector3.forward to spin differently.
  • Make it move with player input: float move = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime; float turn = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime; transform.Translate(0, 0, move); transform.Rotate(0, turn, 0);

Step 8: Understand What’s Happening

  • transform.Translate() moves your object each frame.
  • transform.Rotate() spins it.
  • Time.deltaTime makes motion smooth, no matter your frame rate.
  • OnCollisionEnter() runs automatically when two colliders touch.
  • Debug.Log() prints a message to the Console.
  • GetComponent<Renderer>().material.color changes the object’s color at runtime.

This simple script introduces nearly every foundational concept of Unity scripting: movement, rotation, scaling, physics, and logic. From here, you can branch into player controls, enemy AI, or animations—all built on these same fundamentals.

Share this Doc

Unity Beginner Tutorial: Moving, Rotating, and Scaling Objects

Or copy link

CONTENTS

Chat Icon Close Icon