| Type | Class (Component) |
|---|---|
| Namespace | UnityEngine |
| Inherits | Component > Object |
| In scripts | transform |
Transform
Every GameObject has a Transform. It stores where the object is (position), which way it faces (rotation) and how big it is (scale). It also stores the parent/child relationships you see in the Hierarchy.
World space vs. local space
Giving directions. World space is GPS coordinates: exact and the same for everyone. Local space is "two steps to the left of my desk": it only makes sense if you know where the desk is, and if someone moves the desk, "two steps left of it" moves too. A child object always describes its position relative to its parent's desk.
This is the most important idea on this page.
- World values (
position,rotation) are relative to the scene origin (0, 0, 0). - Local values (
localPosition,localRotation,localScale) are relative to the parent. The Inspector shows local values.
Example: a sword is a child of a hand. sword.localPosition stays at (0, 0, 0.5) while the character runs around, because relative to the hand it doesn't move. sword.position changes every frame. If an object has no parent, local and world are the same.
position is measured from the world origin; localPosition is measured from the parent. Move the Hand and the Sword's world position changes, but its local offset (highlighted) stays the same.Properties
| Name | Type | Description |
|---|---|---|
position | Vector3 | World position. |
localPosition | Vector3 | Position relative to the parent. |
rotation | Quaternion | World rotation. See Quaternion. |
localRotation | Quaternion | Rotation relative to the parent. |
eulerAngles | Vector3 | World rotation as X/Y/Z degrees. Easier to read, but see Common Mistakes. |
localEulerAngles | Vector3 | The rotation numbers shown in the Inspector. |
localScale | Vector3 | Scale relative to the parent. (1, 1, 1) is normal size. |
lossyScale | Vector3 | Approximate world scale (read only). |
forward | Vector3 | The object's blue (Z) axis in world space: the direction it's facing. Length 1. |
right | Vector3 | The object's red (X) axis in world space. |
up | Vector3 | The object's green (Y) axis in world space. |
parent | Transform | The parent, or null. Prefer SetParent to change it. |
root | Transform | The top-most parent. |
childCount | int | Number of direct children. |
Methods
| Method | Returns | Description |
|---|---|---|
Translate(Vector3 translation, Space relativeTo = Space.Self) | void | Moves by an amount. By default in the object's own directions (so Vector3.forward means "its forward"). |
Rotate(Vector3 eulers, Space relativeTo = Space.Self) | void | Rotates by an amount of degrees. |
Rotate(Vector3 axis, float angle) | void | Rotates around a given axis. |
LookAt(Transform target) | void | Turns so forward points at the target. Also accepts a Vector3 position. |
SetParent(Transform parent, bool worldPositionStays = true) | void | Changes the parent. With true, the object stays where it is in the world. With false, it keeps its local values (useful for UI and snapping items to a hand). |
SetPositionAndRotation(Vector3 pos, Quaternion rot) | void | Sets both at once. Slightly faster than setting each. |
GetChild(int index) | Transform | The child at that index (0 is the first). |
Find(string name) | Transform | Finds a direct child by name. Use "Arm/Hand" for deeper paths. Finds inactive children too. |
TransformPoint(Vector3 localPoint) | Vector3 | Converts a point from this object's local space to world space. |
InverseTransformPoint(Vector3 worldPoint) | Vector3 | Converts a world point to this object's local space. Handy for "is it in front of me or behind me?" |
TransformDirection(Vector3 localDir) | Vector3 | Converts a direction (ignores position and scale) from local to world. |
Code Samples
Moving and turning (no physics)
using UnityEngine;
public class TankControls : MonoBehaviour
{
[SerializeField] private float moveSpeed = 5f;
[SerializeField] private float turnSpeed = 120f; // degrees per second
void Update()
{
float move = Input.GetAxis("Vertical");
float turn = Input.GetAxis("Horizontal");
// Moves along the tank's own forward, because Translate defaults to Space.Self.
transform.Translate(Vector3.forward * move * moveSpeed * Time.deltaTime);
transform.Rotate(0f, turn * turnSpeed * Time.deltaTime, 0f);
}
}
Looping through children
// Hide all children of this object.
foreach (Transform child in transform)
{
child.gameObject.SetActive(false);
}
Picking up an item into the player's hand
public void Equip(Transform item, Transform hand)
{
item.SetParent(hand, false); // keep local values, don't keep world position
item.localPosition = Vector3.zero; // snap to the hand
item.localRotation = Quaternion.identity;
}
Is the enemy in front of me?
Vector3 local = transform.InverseTransformPoint(enemy.position);
if (local.z > 0f) Debug.Log("Enemy is in front");
if (local.x < 0f) Debug.Log("Enemy is to the left");
What happens at runtime
- Unity stores only the local values. World values are calculated from the parent chain when you ask for them.
- When you set
position, Unity works backwards to figure out the rightlocalPosition. - Moving a parent moves all its children with it, because their local values stay the same.
- Changes show up immediately. If you set
positiontwice in one frame, only the last value is drawn.
Common Mistakes
transform.position.x = 5;doesn't compile.positionreturns a copy (Vector3 is a struct). Copy it, change it, assign it back:Vector3 p = transform.position; p.x = 5f; transform.position = p;- Doing math on
eulerAngles. Unity may return 350 instead of -10, or flip values like (180, 0, 180). Keep your own angle variable and setrotation = Quaternion.Euler(...)from it. - Moving a Rigidbody with the Transform. Use Rigidbody methods instead; see Rigidbody.
- Non-uniform scale on parents (like 1, 3, 1) makes rotated children look skewed. Scale the child mesh instead of the parent.
See also
This page is a simplified, AI-assisted interpretation. For the complete and authoritative reference, see the official Unity documentation.