How to use Transform Translate in Unity

In Movement by John FrenchPublished Leave a Comment

The Translate function in Unity is a method of the Transform component that moves an object by a set amount, relative to its current position and orientation.

It works by passing in a vector 3 value to represent the movement vector that you want to apply.

For example, passing in a movement vector of two units along the Z-Axis will move an object two units forward.

Like this:

void Start()
{
    // Moves the object forward two units in the direction it's facing.
    transform.Translate(0,0,2);
}

This is different to simply adding a vector to the object’s position to make it move which moves an object relative to its current position in the world.

Instead, Translate will, by default, move an object relative to its own local space.

This means that, when using Translate, passing in a forward vector will move the object in the direction of its blue Z-Axis or, put simply, will move it forwards in whatever direction it happens to be facing.

It’s also possible to force the Translate function to operate in world space instead, by passing in a Space parameter.

Like this:

void Start()
{
    // Moves the object forward two units, no matter which way it's facing.
    transform.Translate(0,0,2, Space.World);
}

However, it’s important to remember that Translate is not necessarily a movement function, it simply changes the position of an object using a vector.

This means that, if you do want to create movement using Transform Translate, you’ll still need to scale the movement vector by Delta Time in order to keep it smooth.

Like this:

void Start()
{
    float moveSpeed = 2;
    // Moves the object forward two units per second.
    transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}

Transform Translate vs Transform Position

Functionally, Transform Translate works in the same way as adding a vector value to the position of an object in order to move it around.

The difference is that, by default, adding to an object’s position moves it in world space, while Transform Translate operates in local space, unless you choose otherwise.

This can be easy to forget, which can cause problems if you try to pass a local transform direction into the Translate function to move an object that is a child of something else.

For example if you have an object that’s rotated by 90 degrees and is facing right, not forward, and you use a global forward vector 3 with the Translate function, it will be converted so that it represents the forward vector of the object.

Meaning that the object will move forward relative to itself, and right relative to the world, even though a global forward vector was used.

Like this:

void Update()
{
    // this object will move forward relative to itself.

    float moveSpeed = 2;
    transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}

However, if you pass the local forward vector of the object in to the Translate function, without specifying the space that you’re operating in, it will still be converted as if it’s a global direction, even though it’s not.

Like this:

void Update()
{
    float moveSpeed = 2;
    transform.Translate(transform.forward * moveSpeed * Time.deltaTime);
}

This can be very easy to do, but means that the direction is, essentially, converted twice, and you’ll get a kind of compound effect where, instead of moving right, the object will move backwards instead.

Typically, you’re most likely to run into this problem when trying to move a child object using the Translate function, such as for a camera rig, for example.

To fix it, use world space vectors with the Translate function or, alternatively, be mindful of the space you’re operating in and set it manually by using the Translate function’s Space parameter.

John French profile picture

by John French

Game audio professional and a keen amateur developer.

Get Game Development Tips, Straight to Your inbox

Get helpful tips & tricks and master game development basics the easy way, with deep-dive tutorials and guides.

How to CODE in Unity. Available to Pre Order Now. Game Dev Beginner.

Leave a Comment