0
0
Unityframework~5 mins

Transform component (position, rotation, scale) in Unity

Choose your learning style9 modes available
Introduction

The Transform component controls where an object is, how it is turned, and how big it is in a game scene.

To move a character to a new spot in the game world.
To rotate a door when it opens or closes.
To make an object bigger or smaller during gameplay.
To set the starting position and size of objects when the game begins.
Syntax
Unity
transform.position = new Vector3(x, y, z);
transform.rotation = Quaternion.Euler(xAngle, yAngle, zAngle);
transform.localScale = new Vector3(xScale, yScale, zScale);

position sets the object's location in 3D space.

rotation sets the object's rotation using angles in degrees.

Examples
Moves the object to the point (0, 1, 0) in the scene.
Unity
transform.position = new Vector3(0, 1, 0);
Rotates the object 90 degrees around the Y axis (turns right).
Unity
transform.rotation = Quaternion.Euler(0, 90, 0);
Scales the object to twice its original size in all directions.
Unity
transform.localScale = new Vector3(2, 2, 2);
Sample Program

This script sets the object's position, rotation, and scale when the game starts. It then prints these values to the console.

Unity
using UnityEngine;

public class TransformExample : MonoBehaviour
{
    void Start()
    {
        // Set position to (1, 2, 3)
        transform.position = new Vector3(1, 2, 3);

        // Rotate 45 degrees around Z axis
        transform.rotation = Quaternion.Euler(0, 0, 45);

        // Scale to half size
        transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);

        // Print current transform values
        Debug.Log($"Position: {transform.position}");
        Debug.Log($"Rotation: {transform.rotation.eulerAngles}");
        Debug.Log($"Scale: {transform.localScale}");
    }
}
OutputSuccess
Important Notes

Changing transform.position moves the object instantly to the new spot.

Use Quaternion.Euler to create rotations from angles you understand (degrees).

localScale changes the size relative to the object's original size.

Summary

The Transform component controls position, rotation, and scale of game objects.

Position moves the object in 3D space.

Rotation turns the object using angles.

Scale changes the object's size.