transform.Translate() Function in Unity

Notes:

transform.Translate() Function in Unity:

Translating game objects using transform.Translate() method:
Translating means changing position of a game object.

transform.Translate():
- is used to set position of a game object or move a game object relative to world, self or other game object’s space.

World space:
Each scene in Unity comes with its own coordinate system; which is called as
World coordinate system (i.e. world space) or
Scene coordinate system (i.e. scene space) or
Global coordinate system (i.e. global space)

The world coordinate system or world space is same for all game objects in the scene

Self space:
Each game object in Unity comes with its own coordinate system; which is called as
Self coordinate system (i.e. self space) or
Object’s coordinate system (i.e. object space) or
Local coordinate system (i.e. local space)

Based on the object’s position, rotation and scale; object’s self coordinate system or self space changes

Syntax: Translate is an overloaded method, has 6 variations

public void Translate(float x, float y, float z)
Moves relative to self coordinate system

public void Translate(float x, float y, float z, Space relativeTo)
where:
relativeTo = Space.Self (default)
Moves relative to self coordinate system
or
relativeTo = Space.World
(Moves relative to world coordinate system )

public void Translate(float x, float y, float z, Transform relativeTo)
where:
relativeTo = otherGameObject.transform
(Moves relative to otherGameObject coordinate system )
or
relativeTo = null
(Moves relative to world coordinate system )

public void Translate(Vector3 translation)
Moves relative to self coordinate system

public void Translate(Vector3 translation, Space relativeTo)
where:
relativeTo = Space.Self (default)
(Moves relative to self coordinate system)
or
relativeTo = Space.World
(Moves relative to world coordinate system )

public void Translate(Vector3 translation, Transform relativeTo)
where:
relativeTo = otherGameobject.transform
(Moves relative to otherGameObject coordinate system )
or
relativeTo = null
(Moves relative to world coordinate system )

Example Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cube1Controller : MonoBehaviour {

public GameObject otherGameObject;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
//this.transform.Translate (0f * Time.deltaTime, 0f * Time.deltaTime, 10f * Time.deltaTime, Space.World);
//this.transform.Translate (0f * Time.deltaTime, 0f * Time.deltaTime, 10f * Time.deltaTime, Space.Self);
//this.transform.Translate (0f * Time.deltaTime, 0f * Time.deltaTime, 10f * Time.deltaTime);

//this.transform.Translate (new Vector3 (0f, 0f, 10f) * Time.deltaTime, Space.World);
//this.transform.Translate (new Vector3 (0f, 0f, 10f) * Time.deltaTime, Space.Self);
//this.transform.Translate (new Vector3 (0f, 0f, 10f) * Time.deltaTime);

//this.transform.Translate (new Vector3 (0f, 0f, 10f) * Time.deltaTime,this.transform.parent);
//this.transform.Translate (0f * Time.deltaTime, 0f * Time.deltaTime, 10f * Time.deltaTime,this.transform.parent);

this.transform.Translate (new Vector3 (0f, 0f, 10f) * Time.deltaTime,otherGameObject.transform);

}
}