7
\$\begingroup\$

I've been tying to understand what the difference between these two methods of moving a rigid body are. Both seem to move the object at a constant rate and both operate on a RigidBody. So what's the difference between the two and when should one be used over the other?

\$\endgroup\$
15
\$\begingroup\$

transform.translate is not going to utilize the physics system (doesn't look for collisions when moving, ignores any current velocity, etc.), nor will it nessesarily be smooth movement (since you can translate as far as you like). This is essentially setting a new location for the entire object, relative to the current position. This would be how you should move objects if you don't want to use physics. Either because the object doesn't have physics, or you want to ignore physics (like teleporting).

rigidbody.velocity uses the physics system, and will be mostly smooth (except for extreme velocities). This is more like telling the objects which direction to move, instead of telling them what position be at. Movement here will consider the physics world around it. If you set a velocity towards a solid wall, the physics system will attempt to make that move, and adjust the velocity based on the collision with the wall. In many cases, you're more likely to leave the velocity alone and adjust the object's acceleration instead, likely by applying a force.

\$\endgroup\$
3
\$\begingroup\$

They do rather different things, but can be used to achieve the same results.

Setting the body's velocity sets its speed and direction. As long as the body is being updated by the physics system, it's velocity will be (effectively) added to its position in order to arrive at a new position for the frame. This causes the object to move at constant speed over time until you change the velocity to something else, or to zero.

The Translate method of the Transform type adjusts the translation stored in the body's transformation matrix by adding the provided translation to it. This causes the object to move instantly to the new position, once, ignoring the physics system. This will not cause the object to move over time unless you constantly call it every frame.

Use the one that makes the most sense for the semantics you want when interacting with the object. If you just want to position it once (or occasionally) and leave it there, using translate is fine. If you want it to move constantly, and nicely interact with other physical objects, setting velocity is probably less work overall than manually translating it every frame yourself.

\$\endgroup\$
0
\$\begingroup\$

The difference starts to matter when you have your rigidbodies collide with each other.

Changing the transform "teleports" the object to a different location without giving it any kinetic energy. It won't interact with any objects on the way. But moving an object by giving it a velocity means that it will push other objects out of the way, which accelerates the other objects and decelerates the object itself.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.