Menu
CoddyTech

Local and World Space

Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 19 of 67.

An object has its own directions as well as the world's. transform.forward is where the object's nose points (the blue arrow in Unity's Scene view), given as a world vector one unit long; transform.right and transform.up are its own right and up. Turn the object and they turn with it. This paper plane is turned 90 degrees around y in the scene, so it flies along the world's x, the way it points:

public float speed = 4f;

void Update()
{
    transform.position += transform.forward * speed * Time.deltaTime;
}

transform.Translate(Vector3.forward * speed * Time.deltaTime) does the same thing, because Translate reads its vector in the object's own space: there, Vector3.forward means the object's forward. Pass Space.World as a second argument and it uses the world's axes instead, so transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.World) moves along the world's z whichever way the plane points.

transform.Rotate also turns around the object's own axes. Seen from above, a positive angle around y turns an upright object to its right and a negative one to its left, and its transform.forward turns with it.

challenge icon

Challenge

Easy

Give the tank tank controls: the right arrow turns it right and the left arrow turns it left, around its y axis at turn degrees per second. The up arrow drives it forward, the way it faces, at speed units per second. Turning alone does not move it.

Press Play, turn and drive, then press Check.

Try it yourself

using UnityEngine;

public class Tank : MonoBehaviour
{
    public float speed = 3f;
    public float turn = 90f;

    void Update()
    {
        // Right/left arrows: turn around y. Up arrow: drive the way the tank faces.
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unity Scripting Basics