In the previous lesson we made our character rotate using the mouse and the Transform.Rotate method. This time around, we will modify our movement script to ensure our character moves in the direction she is pointing.
Pushing right on our keyboard currently moves our character along the world x axis, regardless of what direction she is pointing in. Likewise the up and down keys move her along the world z axis. This is because the Vector3 we supply to our Move method moves her forwards, back and side to side in world space. Instead, we want her to move based on the direction she is facing.
To understand local and world space, you can think of each object in Unity as having their own little world. When you child an object to another, the parent becomes it’s entire world. Last lesson we set the Nose object to position 0, 0.5, 0.5. Select the character in the editor and move her around. You’ll notice that her position changes as you move her, but if you one again select the Nose you’ll see that it is still at 0, 0.5, 0.5. This is because as far as the Nose is concerned it has not moved within it’s world. Rotating or scaling the character has the same effect. To visualize this, you can imagine a grid attached to the character that moves and rotates with it; no matter where the character goes, the grid is always rooted at the origin, and objects stay in the same place along it.

Diagram showing world space and local space. The blue sphere is a child of the white sphere; it’s local position is shown in red, while the white sphere’s world position is in green. Notice how when the white sphere is moved and rotated, the blue sphere’s local position does not change.
In Unity, Vector3s are most often used to represent points or directions in space. There isn’t any difference between the two from the engine’s point of view—it’s just a useful way to think about vectors. When we translate any object in space, you can imagine the direction of movement as an arrow pointing from the start position to the end position. Just like points, directions can be in local or world space.
Right now, if we push the up arrow our movement vector is 0, 0, 1, or “straight forward in the world.” Our desired behaviour is “straight forward from the direction our character is pointing.” CharacterController.Move takes a vector in world-space, but the one we have is local to the direction our character is facing. Let’s change that. Replace the Move line with the following.
gameObject.GetComponent<CharacterController>().Move(transform.TransformDirection(input * speed * Time.deltaTime));
We’ve added a new method call, once again to the Transform object attached to our character. TransformDirection is a method that takes in a direction local to the transform, and returns a direction in world space.
Save your script and run your game, and you’ll see your character move the way she’s pointing!
This is what I see:
“Just like points, directions can be in local or world space.
“IMG
“Right now, if we push the up arrow…”
Looks like there was supposed to be an image there?
I typically used IMG as a placeholder to where I thought an image would be necessary while writing the tutorials (since I would write them, then take pics after), so it looks like that one didn’t get caught. Thanks for the heads up!
Thank you, Just thank you so much.