Local Space Vs. World Space in Unity
In the previous blog, the movement and rotation of the player was set up, but there is an issue: the player is moving towards a different direction that it is facing, for instance, to move backwards in the scene when we are attempting to move forward. So we need to understand the difference between world space and local space in Unity
Let’s see some images
Player is the orange triangle. blue is forward direction and red is right direction. For now they are facing the same direction, but what happens if I rotate the player?
The player is still facing it’s own forward direction, but in the Global coordinates, he’s facing the right direction
Let’s read this text from Unity forum:
“Imagine you’re in a car. You have a position and rotation relative to in the car. You’re in the driver seat facing out the window. BUT, as the car drives around, and turns, your position on earth is changing, as well as your cardinal facing direction, but your position within the car has not changed”
Let’s imagine that the triangle is the driver
Imagine that the car is turning left, but the driver decides to turn himself to the right… That’s weird
Therefore, in some cases we need to convert from local space values to global ones or viceversa. In the last case, we want that the car’s rotation to be applied also to the driver’s rotation, so both are facing the same global direction
In the previous blog, the camera is the car and the player is the driver. That’s why the player moves in a different direction than the one the camera is facing
In this case we need to use Transform.TransformDirection. As said there, it Transforms direction
from local space to world space
Now it’s working fine