TExcelTAD
Home › Unity with C# › Transform & Coordinate Spaces

Transform & Coordinate Spaces

Every GameObject has exactly one Transform: a position, a rotation and a scale, relative to its parent. Parenting a sword to a hand bone, a turret to a tank, or a camera to a player all rely on that one rule: a child's values are local, and the engine combines them with every parent above to get the world values.

Local and world properties

Local (relative to the parent) World
transform.localPosition transform.position
transform.localRotation transform.rotation
transform.localScale transform.lossyScale (read-only, approximate with skewed parents)

Directions follow the object's rotation: transform.forward (+Z), transform.right (+X) and transform.up (+Y) are world-space unit vectors. Unity uses a left-handed coordinate system with Y up, and measures angles in degrees in the API.

csharp
using UnityEngine;

public class Turret : MonoBehaviour
{
    [SerializeField] Transform barrelTip;      // a child object at the end of the barrel
    [SerializeField] Transform target;
    [SerializeField] float turnSpeed = 120f;   // degrees per second

    void Update()
    {
        // Turn toward the target around the Y axis only.
        Vector3 toTarget = target.position - transform.position;
        toTarget.y = 0f;
        if (toTarget.sqrMagnitude < 0.0001f) return;
        Quaternion wanted = Quaternion.LookRotation(toTarget);
        transform.rotation = Quaternion.RotateTowards(transform.rotation, wanted, turnSpeed * Time.deltaTime);
    }

    public Vector3 MuzzleWorldPosition() => barrelTip.position;               // world space
    public Vector3 PointInFront(float metres) => transform.TransformPoint(0f, 0f, metres);
}

Useful conversions:

  • TransformPoint(local) / InverseTransformPoint(world): positions (affected by position, rotation and scale).
  • TransformDirection(local) / InverseTransformDirection(world): directions (rotation only).
  • transform.Translate(v) moves in local space by default; transform.Translate(v, Space.World) moves in world space.
  • transform.SetParent(newParent, worldPositionStays: true) keeps the object where it is on screen; false keeps its local values and makes it jump.

A model of parent and child

The model below works in the XZ plane with yaw rotations (turning around Y) and uniform scale, which is enough to see how local values become world values. A tank sits at (10, 0, 5) facing east (yaw 90°); its turret is a child 2 units ahead and to the left.

csharp · try it yourself
using System;

var tank = new Node("Tank") { LocalX = 10, LocalZ = 5, LocalYaw = 90 };
var turret = new Node("Turret", tank) { LocalX = -1, LocalZ = 2, LocalYaw = 0 };

Print(tank); Print(turret);

tank.LocalYaw = 180;                          // the tank turns to face south
Console.WriteLine("-- tank turns to yaw 180");
Print(turret);

var (lx, lz) = tank.InverseTransformPoint(10, 0);
Console.WriteLine(
quot;world (10, 0) in tank space: ({lx:F2}, {lz:F2})"); static void Print(Node n) { var (x, z) = n.WorldPosition(); Console.WriteLine(
quot;{n.Name,-6} local ({n.LocalX,5:F2}, {n.LocalZ,5:F2}) world ({x,5:F2}, {z,5:F2}) yaw {n.WorldYaw(),3:F0}"); } class Node { public string Name; public Node? Parent; public double LocalX, LocalZ, LocalYaw; // yaw in degrees, clockwise seen from above (Unity's +Y rotation) public Node(string name, Node? parent = null) { Name = name; Parent = parent; } public double WorldYaw() => (Parent?.WorldYaw() ?? 0) + LocalYaw; // Unity rotates +Z (forward) toward +X (right) for a positive yaw. static (double, double) Rotate(double x, double z, double yawDeg) { double r = yawDeg * Math.PI / 180; return (x * Math.Cos(r) + z * Math.Sin(r), -x * Math.Sin(r) + z * Math.Cos(r)); } public (double, double) TransformPoint(double x, double z) { var (rx, rz) = Rotate(x, z, LocalYaw); double wx = LocalX + rx, wz = LocalZ + rz; return Parent == null ? (wx, wz) : Parent.TransformPoint(wx, wz); } public (double, double) WorldPosition() => Parent == null ? (LocalX, LocalZ) : Parent.TransformPoint(LocalX, LocalZ); public (double, double) InverseTransformPoint(double wx, double wz) { if (Parent != null) (wx, wz) = Parent.InverseTransformPoint(wx, wz); return Rotate(wx - LocalX, wz - LocalZ, -LocalYaw); } }
Tank   local (10.00,  5.00)  world (10.00,  5.00)  yaw  90
Turret local (-1.00,  2.00)  world (12.00,  6.00)  yaw  90
-- tank turns to yaw 180
Turret local (-1.00,  2.00)  world (11.00,  3.00)  yaw 180
world (10, 0) in tank space: (0.00, 5.00)

The turret's local values never changed, but its world position swung around the tank. The last line goes the other way: the point (10, 0) lies 5 units straight ahead of the south-facing tank. The values are printed rounded; the real results carry tiny floating-point errors such as 6E-16, just as the Inspector sometimes shows -1.5e-07 instead of 0.

Rotations: use quaternions, edit with Euler angles

Unity stores rotations as Quaternions. The Inspector shows Euler angles because they're readable, but adding to transform.eulerAngles every frame can flip or jitter near ±90° pitch (gimbal lock). In code:

  • build rotations with Quaternion.Euler(x, y, z), Quaternion.LookRotation(forward), Quaternion.AngleAxis(deg, axis);
  • combine with * (transform.rotation = yaw * pitch);
  • interpolate with Quaternion.Slerp or Quaternion.RotateTowards;
  • transform.Rotate(0, degrees, 0) adds a rotation in local space.

The math behind them is in Math for Games.

Performance notes

  • Setting position and rotation separately does two hierarchy updates. Use transform.SetPositionAndRotation(p, r) (or SetLocalPositionAndRotation) when changing both.
  • Deep hierarchies make every transform change more expensive, because children must be updated too. Don't parent hundreds of moving objects under one "Enemies" folder object at runtime; keep moving objects near the root.
  • Don't move objects with a Rigidbody through transform. See Rigidbody & Forces.

Test yourself

A child has localPosition (0, 0, 2). Its parent is at (5, 0, 0) with no rotation and scale 3. Where is the child in world space?

  1. (5, 0, 2)
  2. (5, 0, 6)
  3. (0, 0, 2)
  4. (15, 0, 6)

Which call converts a direction from local to world space without applying position or scale?

  1. TransformPoint
  2. TransformDirection
  3. InverseTransformPoint
  4. Translate

You reparent a pickup to the player's hand and want it to stay exactly where it is visually. Which call?

  1. SetParent(hand, false)
  2. SetParent(hand, true)
  3. transform.parent = null
  4. DontDestroyOnLoad(hand)