Transform
TypeClass (Component)
NamespaceUnityEngine
InheritsComponent > Object
In scriptstransform

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.

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.

XYWorld origin (0, 0, 0)hand.position (world)sword.position (world)sword.localPosition(offset from the Hand)Hand (parent)Sword (child)
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

NameTypeDescription
positionVector3World position.
localPositionVector3Position relative to the parent.
rotationQuaternionWorld rotation. See Quaternion.
localRotationQuaternionRotation relative to the parent.
eulerAnglesVector3World rotation as X/Y/Z degrees. Easier to read, but see Common Mistakes.
localEulerAnglesVector3The rotation numbers shown in the Inspector.
localScaleVector3Scale relative to the parent. (1, 1, 1) is normal size.
lossyScaleVector3Approximate world scale (read only).
forwardVector3The object's blue (Z) axis in world space: the direction it's facing. Length 1.
rightVector3The object's red (X) axis in world space.
upVector3The object's green (Y) axis in world space.
parentTransformThe parent, or null. Prefer SetParent to change it.
rootTransformThe top-most parent.
childCountintNumber of direct children.

Methods

MethodReturnsDescription
Translate(Vector3 translation, Space relativeTo = Space.Self)voidMoves by an amount. By default in the object's own directions (so Vector3.forward means "its forward").
Rotate(Vector3 eulers, Space relativeTo = Space.Self)voidRotates by an amount of degrees.
Rotate(Vector3 axis, float angle)voidRotates around a given axis.
LookAt(Transform target)voidTurns so forward points at the target. Also accepts a Vector3 position.
SetParent(Transform parent, bool worldPositionStays = true)voidChanges 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)voidSets both at once. Slightly faster than setting each.
GetChild(int index)TransformThe child at that index (0 is the first).
Find(string name)TransformFinds a direct child by name. Use "Arm/Hand" for deeper paths. Finds inactive children too.
TransformPoint(Vector3 localPoint)Vector3Converts a point from this object's local space to world space.
InverseTransformPoint(Vector3 worldPoint)Vector3Converts a world point to this object's local space. Handy for "is it in front of me or behind me?"
TransformDirection(Vector3 localDir)Vector3Converts 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

  1. Unity stores only the local values. World values are calculated from the parent chain when you ask for them.
  2. When you set position, Unity works backwards to figure out the right localPosition.
  3. Moving a parent moves all its children with it, because their local values stay the same.
  4. Changes show up immediately. If you set position twice in one frame, only the last value is drawn.

Common Mistakes

See also