DEV Community

Cover image for Moving Objects in Unity The Right Way
EliteSalad
EliteSalad

Posted on • Updated on

Moving Objects in Unity The Right Way

Physics based

Rigidbody SetVelocity

rb.velocity = transform.forward;
Once velocity is set provided there is no drag object will maintain pace until collision.
Great for launching an object with a speed independent of its mass

Rigidbody MovePosition

rb.MovePosition(transform.position + (transform.forward * Time.deltaTime));
Only physics based way to move a kinematic object
Stable Way to move a rigidbody object without manipulating the physics of the object just the position.

Rigidbody AddForce

rb.AddForce(transform.foward * Time.deltaTime );
Every frame force will be added leading to acceleration up to infinity provided drag is 0.
Allows for specific force modes to be inserted such as Start, Impulse, Acceleration, Force, and Velocity Change. These can all change the behavior of how that particular force is applied to the object.

Position Based

Transform Translate

transform.Translate(Vector3.foward * Time.deltaTime);
Gives an amount of space to move each frame
Great for animating
Can move kinematic objects

Transform Set Position

transform.position += transform.forward * Time.deltaTime;
Giving new position to move every frame
Great for animating
Can move kinematic objects

Because movement operations through transform happen asynchronously when moving through or next to an object with a collider on the object may bounce or stutter as it tries to continue in the desired direction. This is because it successfully moves before being rejected back out by the physics of the collider. Because of this using these functions on an object that is meant to touch another object in some way is not recommended.

Discussion (0)