About Local Space and World Space- Game Dev Series 121

Objective: using 3rd person Player movement to explain the difference of local space and world space in Unity.

When create a Player like 3rd person character, especially in 3D game, would usually face a problem of movement and rotation error. Most of these problems are all about the issue of local space and world space.

In some games, when the movement space or direction would be only one, these games would not have the issue of local vs world space. For example, the 2D galaxy shooter game, the entire interact-able space would be only one, and the direction is absolutely clear.

The direction is simple and absolutely.

But games like 3rd person shooter game, the direction would depend on the mouse movement. By that time, you need to make a clear direction order in local space or in world space. Otherwise you might be only walking on the absolute X, Y, and Z-axis.

An easy way to check the angle difference between local and world space.

When facing these kind of problems on setting Player movement and rotation, here are some tips to fix it.

Rotation from world rotation to local rotation

When using mouse as view point control, need to careful of not controlling on the world space axis.
Here are the demo code of controlling the Y-axis rotation by using mouse:

In these lines of code, first we receive the rotation of camera, then control it by using mouse input, and set the rotating angle back to camera rotation.
With these codes, this is what would happen when Play it:

You can see the camera has always facing Z-axis and rotating on the Y-axis in world space. Which is completely not we want.
To fix this, make the mouse input value would only effect on the local rotation. We can simply do it by using transform.loralRotation.

Play it again and you should see the difference.

A perfect rotation in local space.

Movement from local space to world space

Once we are able to rotating the camera properly, the next part would be walking toward the direction where Player facing.
Create a simple movement function by using CharacterController.Move under Update().

However, it would become a movement issue like this:

You would see that Player has always walk on Z-axis as forward and would never walk toward the direction of facing.
That is because the value of velocity is in world space, which means the Z-axis would technically the “right” direction when we pressed “forward”.
To fix this, we need to change the value in local space turn into world space, and we could use transform.TransformDirection(Vector3).

And now your Player can walk toward the direction it is facing.

A passionate gamer whose goal is to work in video game development.