movement -- add to position vs. Translate method

I am attempting to move an object. After rotation about the object’s Y-axis via the code:

transform.Rotate( 0, rotateSpeed * Time.deltaTime, 0 );

I can move the object correctly using the code:

transform.position += transform.forward * 10.0f * Time.deltaTime;

which works as expected, whereas the code:

transform.Translate( transform.forward * 10.0f * Time.deltaTime );

does not; in particular, the latter version moves the objected in unexpected ways, as if the transform.forward vector is not pointing where I am expecting it to. Can anyone explain the difference between these two approaches to movement? In particular, I was surprised that they did not have the same effect.

If you look at the reference for Transform.Translate() you will see there is an optional second parameter ‘relativeTo’. The default is Space.Self, which means the movement used with be a local rather than world. You can fix the problem with your use of Translate in two ways. First you can pass a local coordinate to the Translate() function:

 transform.Translate( Vector3.forward * 10.0f * Time.deltaTime );

Note the use of ‘Vector3.forward’ rather than ‘transform.forward’. The second way is to change the ‘relativeTo’ parameter:

transform.Translate( transform.forward * 10.0f * Time.deltaTime, Space.World);

If you are applying position taking into account current position of you gaming object (I mean .position += newPosition and not .position = newPosition) then you could easily test these two aproaches simpy by rotating your game object.

So basically transform.Translate will move you object using .position += (so from current position of GO) and move it to a direction where X is currently pointing (assuming you are moving your object on X axis). However if you add one more argument when calling Translate method Space.World then it will work exactly the same way as .position += newPosition and instead of using local X (where your GO’s X is pointing) it will use worlds X. And so on for all the other axises, so transform.Translate takes local rotation into account when moving a game object.