CodeBlanks Logo Icon CodeBlanks

Moving with Translate

Learn relative motion with transform.Translate.

Moving with Translate

transform.Translate is used to move an object relative to its current position. The Vector3 values you provide are added to the object's movement. This allows you to move an object by a certain amount instead of assigning an absolute position.

transform.Translate(1, 0, 0);

This example moves the object one unit along its movement directions.

Understanding Relative Movement

Movement performed with Translate starts from the current position and adds the provided amount to it. For example, transform.Translate(0, 1, 0) moves the object one unit along the Y direction. You can perform the same operation by passing a Vector3 to the method.

Vector3 movement = new Vector3(0, 0, 3);
transform.Translate(movement);

By default, Translate operates relative to the object's local coordinate directions.

Practice Move an Object with Translate

Inside the Start method, move the object three units along the X axis, zero units along the Y axis, and two units along the Z axis from its current position. Use transform.Translate and Vector3.

Starter code

Player.cs
using UnityEngine;

public class TranslateMover : MonoBehaviour
{
    void Start()
    {
        // Move the object relative to its current position.
    }
}

Sample solution

Player.cs
using UnityEngine;

public class TranslateMover : MonoBehaviour
{
    void Start()
    {
        transform.Translate(new Vector3(3, 0, 2));
    }
}

Expected output

(3.00, 0.00, 2.00)

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which code moves an object two units along the X axis from its current position?

  • transform.position = new Vector3(2, 0, 0);
  • transform.Translate(0, 2, 0);
  • transform.Translate(2, 0, 0); Correct answer
  • transform.localScale = new Vector3(2, 0, 0);

Explanation: Translate(2, 0, 0) moves the object two units along the X axis from its current position.

2 Set the Relative Movement Amount

Question
transform.Translate(new Vector3(0, ____, 0));
Answer
transform.Translate(new Vector3(0, 4, 0));

Explanation: The second Vector3 value represents the Y axis. Enter 4 for a movement of four units.

3 Translate Movement

Term Meaning
transform.Translate Moves the object relatively
new Vector3(1, 0, 0) Specifies one unit along the X axis
new Vector3(0, 1, 0) Specifies one unit along the Y axis
new Vector3(0, 0, 1) Specifies one unit along the Z axis

All lessons in Beginner