There are many ways to move an object in Unity. What is the difference between these approaches?
transform.Translate (0, 0, speed * Time.deltaTime);
GetComponent & lt; Rigidbody2D & gt; (). Velocity = new Vector2 (0, v);
GetComponent & lt; Rigidbody2D & gt; (). AddForce (0, 0);
Did I understand correctly that 1 is a linear movement, 2 is a movement with constant acceleration, 3 is a movement with a smooth increase in speed and a soft stop?
Answer 1, authority 100%
To better navigate the question, see the following 2 questions:
- Correct implementation of character movement
- Collision movement through Update and FixedUpdate, what is“ teleportation ”in the context of a physics engine in Unity?
Now, briefly on your question:
Translate ()
– moving the character by simply changing its coordinates, or rather, shifting the position of the object relative to its current position. It is rather not a linear movement, but “teleportation” of an object, since the object dramatically changes its position in space. This is not a physical way of moving, that is, you should resort to this method only when you are not using physics to move the object, otherwise problems with object collisions may occur (for example, despite the correctly configuredRigidbody
andCollider
one object can get stuck in another. In addition, you should use such movement only in theUpdate ()
method, and notFixedUpdate ()
. The lower the value you pass it as a parameter, the less “jerky” and smooth your movement will look (but in fact, it will still remain a teleportation)Rigidbody2D.velocity
– instant change in the speed of a physical object. Should be used with caution as it can lead to physically unrealistic behavior. In general, when using physics in Unity, the speed of the object will be calculated taking into account all the forces that act on the physical body and the mass of the physical object. Therefore, if it turns out that the physics engine calculates the speed of the object for you according to all the forces that act on it, and then you change it with your hands, using the means of changingRigidbody2D.velocity
– it may look wrong. This method should be used only in exceptional cases when it is necessary to drastically change the movement of an object (Unity themselves in docs give an example of jumping a character). It should be used only inFixedUpdate ()
, and in no case inUpdate ()
, and also avoid changing the position of the character (transform.Translate ()
and changingtransform.position
)Rigidbody2D.AddForce ()
– add force action to an object. As already mentioned in the previous paragraph, the physics engine calculates the speed and direction of motion of an object based on the forces that act on the object, as well as its mass. With this method, the “acceleration” of a physical object will be smoother. To make an analogy – applying this method – as if you pushed a physical object in a certain direction. It should be used only inFixedUpdate ()
, and in no case inUpdate ()
, and also avoid changing the position of the character (transform.Translate ()
and changingtransform.position
)