Cristian Garcia
Cristian Garcia

Reputation: 9859

rigidbody.velocity vs lerp efficiency in Unity3D

I have two ways of moving GameObjects:

Method 1

void Update(){
    rigidbody.velocity = ( targetPosition - transform.position )*speed;
}

Method 2

void Update(){
    transform.position = Vector3.lerp( transform.position, targetPosition, Time.delta*speed);
}

Which one is more efficient? Does rigidbody.velocity use the GPU?

Edit

I develop for mobile platforms so I would like to know if performance might vary considerably between the two methods.

Upvotes: 2

Views: 5346

Answers (1)

ssell
ssell

Reputation: 6597

The answer to the question

Which one is more efficient?

is transform.position

This is because using transform.position directly modifies the position of the GameObject's Transform component which controls the position, orientation, and scaling of said object.

Using rigidbody.velocity modifies this transform using the Physics system which can be slower than just directly setting the value.

But I have to stress that this is an extreme case of premature optimization, and the difference between the two in terms of performance is negligible. The only difference you should be concerned about is how using these different methods to move objects fits into your game design.

Typically, transform.position should only be used in two cases: (1) instantaneous transportation (spawning, teleportation, etc.) or (2) objects that do not use the physics system (ie don't move based on gravity, friction, or other factors).

On the other hand, using rigidbody.velocity is used to provide more realistic movement behavior based on the physics system.

Which one to use should be based on the design of the game and the current object, and not be determined by "which one is more efficient?"

Upvotes: 3

Related Questions