Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 28, 2021 11:05 pm GMT

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.


Original Link: https://dev.to/xavieroliver/moving-objects-in-unity-the-right-way-3jkb

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To