In this lesson, you will learn how to accurately calculate where an object exists in the game world relative to its parent’s orientation using TransformPoint and TransformDirection.
Why coordinate spaces matter
Imagine you are building a car game. You want to spawn a "muzzle flash" effect exactly two units in front of the car’s hood. If you only look at the car’s position in world space, you won't know which way the car is pointing. If the car turns, that "front" direction changes. To solve this, we convert local coordinates (the car's perspective) into world coordinates (the scene's perspective).
Calculating positions with TransformPoint
TransformPoint takes a local position and converts it into a world-space position, accounting for the object's current rotation and scale.
If you want to spawn an item 2 units in front of an object, you define that point in local space—(0, 0, 2)—and pass it through the object’s transform:
// Define a local point 2 units in front of this object
Vector3 localPoint = new Vector3(0, 0, 2);
// Convert that local point to world space
Vector3 worldPoint = transform.TransformPoint(localPoint);
// Now you can spawn an object at that exact world position
Instantiate(muzzleFlashPrefab, worldPoint, transform.rotation);
Calculating directions with TransformDirection
Sometimes you don't need a specific point; you just need to know which way "forward" is relative to an object. TransformDirection is perfect for this. While TransformPoint cares about the object's position, TransformDirection only cares about the rotation.
If you want to apply a force to a projectile so it moves in the direction the object is currently facing, use this:
// Get the "forward" direction relative to the object's rotation
Vector3 worldDirection = transform.TransformDirection(Vector3.forward);
// Apply this direction to a Rigidbody
myRigidbody.velocity = worldDirection * speed;
By using these methods, your objects will behave consistently, regardless of how they are rotated or scaled within the scene hierarchy. You no longer have to manually calculate complex trigonometry to find where things should be; Unity handles the math for you.
Key Takeaway: Use TransformPoint to translate local positions into world coordinates, and TransformDirection to convert local vectors into world-oriented directions.