- Home /
 
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.
Answer by robertbu · May 21, 2013 at 06:38 PM
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);
 
              To see if I am understanding correctly: the Translate method considers any vector argument to be in global coordinates, then converts it into local coordinates, but transform.forward is already in pointing in the correct direction, and so it gets unnecessarily transformed, causing it to point in the wrong direction?
Thanks for your help in understanding!
Yes. In other words, you have to correct for your forwards maybe being a diagonal, but your version double-corrected for it.
trans.Translate accounts for your personal spin, and trans.forward also accounts for your personal spin. That makes sense, since transform means "me," and saying "my forward" should mean your personal blue arrow.
Using both is like trying to account for a 3-hour time-zone difference by running through a converter program and also adding 3 hours.
@Stemkoski - No. Translate() by default, considers any argument to be in local coordinates and translates it to world coordinates. 'transform.forward' is already in world coordinates. So @Owen Reynold's analogy of converting twice for a time zone change is apt.
Answer by evgeny0191 · Feb 12 at 01:31 PM
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.
Your answer
 
             Follow this Question
Related Questions
how do i slowly translate a object to a other objects position 2 Answers
Object keeps orbiting target location in a Coroutine 0 Answers
How do I translate my ship left and right and have it rotate at the same time? 1 Answer
[Solved] Translating Camera on only X, Z axis on World Space, no matter what is the rotation 0 Answers
Object moving between waypoints is jerky 0 Answers