Unity — Object movement

Benjamin Calvin
3 min readSep 3, 2021

--

Objective: Explain the transform class through position property and translate method. Explain why to use one over the other. Use Instantiate and random with a game object, to flood the screen with random cubes.

While going through my adventure in game development, I tried both transform.position and transform.Translate. Both method and property provide the same result.

transform.position += Vector3.up * speed * Time.deltaTime;transform.Translate(Vector3.up * speed * Time.deltaTime);

What’s the difference? Let’s look at each set up in the unity documentation.

transform.translate()

A public method of transform that moves the transform in the direction and distance of translation.

transform.position

A property of transform of the world space position of the Transform.

I see one computes direction and distance while the other provides world space position. They both provide the same result, so why not just pick a random one? From Microsoft Developer Network(MSDN) states:

In general, methods represent actions and properties represent data. Properties are meant to be used like fields, meaning that properties should not be computationally complex or produce side effects. When it does not violate the following guidelines, consider using a property, rather than a method, because less experienced developers find properties easier to use.

Does the use of transform.position make this more computationally complex or produce side effects? Yes, because we are passing in multiple properties and methods into a single transform property. transform.translate() should be used.

Random dropping Unity objects.

Setup random respawn locations

The game environment is set up, the object of choice has a Rigidbody attached, and in this instance, gravity == false;

Here is an example of using transform.translate(Vector3.down * (speed variable) * Time.deltatime on an object.

If the object reaches a certain transform.position.y, it will utilize a new Vector3 to move the object to a new position, in this case, to the top of the screen.

Just for fun, let’s make it rain by creating a GameObject, using Instantiate(In-STAN-shi-ate), and Random.Range function. Be careful because this will crash Unity. Don’t forget to assign your prefab to the game object in the inspector!

[SerializeField]                                               private float _speed = 4.0f;
private float _randomize;
[SerializeField]
private GameObject _makeItRain;
void Update()
{
transform.Translate(Vector3.down * _speed * Time.deltaTime);
_randomize = Random.Range(-9.0f, 9.0f);
if (transform.position.y <= -6)
{
Instantiate(_makeItRain, transform.position = new Vector3(_randomize, _randomize, 0), Quaternion.identity);
}
}
make it rain, make it, flood?

Though the object was not rotating, Instantiate still requires it, hence the Quarternion.identity. The identity property represents that non-rotational item. I utilized Random for both X and Y hence why it spawned in random locations on the screen.

One last thing I want to make the game select a random color for each new spawn.

_makeItRain.GetComponent<Renderer>().material.color = _randomColor;

Accessing the color property of the gameobject and making a new color object. Here is the result.

taste the cubed rainbow

--

--

Benjamin Calvin

Dedicated and motivated individual learning programming and sharing my discoveries with you.