Local Space vs. World Space for 3d Objects in Unity
In 3d applications, including Unity, it is important and foundational to understand the difference between local space and world space. Here I will talk briefly about each one and what the difference is without going into the math behind it.
Basically, there is only one world space per scene. This is the global coordinate system and axes by which we track the object’s position in the world, or a scene in the case of Unity. Each object’s position has a world space location defined in world space X, Y, Z.
If you look at the upper right corner, you will see a gizmo with a green tip on the top and a red tip on the right. A blue tip is coming off of the front of the gizmo (currently can’t be seen). The green represents the positive world Y Axis, the red is the positive world X Axis, and if you could see it, the blue would be the positive world Z Axis. Our camera is currently aimed down the world’s positive Z Axis.
If you watch the Gizmo as the scene is rotated, you will notice that the X, Y, and Z axes continue to point in the same direction, kind of the way a compass needle always points to magnetic north. This helps to quickly tell our orientation in relation to these world Axes.
Now let’s discuss local space. Each object has it’s own local coordinate system. These are the coordinates relative to the object’s local origin. See the picture below:
The cube has its own X, Y, and Z Axes, which run through the middle of the cube, making the local origin the middle point of the cube. Watch below in Unity as the object is rotated in the world, it’s local axes rotate also:
Because we are talking about 2 different coordinate systems, rotating an object around it’s local Y Axis is not the same as rotating it around the world Y Axis. When the object is initially placed in the scene, its local coordinate system lined up with the world coordinate system. Once we rotate the object that is no longer the case. What is forward in local space is no longer forward in world space.
What happens if we want to move the object in its forward direction and not the world forward direction (local Z Axis vs. World Z Axis)? We will need to convert the forward vector of the object to a world space vector which points in the same direction as the local space vector.
Unity provides a simple method for this: transform.TransformDirection(direction)
This method takes the local direction as a parameter and returns a vector in world space representing the same direction. This allows us to move the object forward in world space in a direction equal to the local forward direction of the object.
See below:
Above is what happens when we move the object forward without transforming its forward direction to world space. It’s forward direction is the blue arrow. You can see the movement is in the world space forward direction, not taking into account that the local forward is actually different than the world forward. What we want is the object to move in the direction of its blue arrow when I push the forward key. Enter TransformDirection().
Now we use TransformDirection(direction) to convert the local forward to its equivalent in world space:
You can now see that as I push the forward button, the object moves in the proper direction, which is the direction of its local forward vector. The vector just needed to be converted to a world space equivalent, which is done with the help of TransformDirection(). Note that somewhere under the hood, transforming from local to world space requires a matrix multiplication. Luckily Unity takes care of that for us.