No transform.left?

Why is there transform.forward, transform.right and transform.up

But no transform.left, transform,down or transform.back??

And on a more practical question;

Is this the most efficient way of getting an object to move in your game:

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

There is no Transform.left/down/back, because those are just negatives of the existing ones. For example, "-transform.right" is the same as the imaginary "transform.left". You can use extension methods to add these to your transforms.

About the translation, I think that's pretty much it. It's good to always use a predefined vector, then lengthen it by multiplying as you see fit.

4 Likes

Thank you Blizzy

Its also generally a good idea to expose your speed variable to the inspector. Saves a lot of dropping back and forth to the code every time you want to tune it.

So the above would become

[SerialiseFeild] float moveSpeed = 2;

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

The code is no more or less efficient then before, but you as a game developer will work much faster if values can be adjusted from the editor rather then being hard coded. It also makes it more obvious what the 2 does in six months time. It also lets you work with non programmers if you ever join a team.

For a more detailed discussion google 'magic numbers'

2 Likes