3 added 22 characters in body
source | link

By default, rigidbodies in Unity behave according to Newton's First Law of Motion:

An object continues to move at a constant velocity, unless acted upon by a force.

So when you add a force to an object, then it gains a velocity and retains that velocity until something slows it down.

But this is not the case if you manipulate the velocity directly. Assigning a new Vector2 to your velocity cancels any velocity the object had before. So with your Move2 script, the object will never retain a velocity between frames, because when the user does not do any input, then the velocity will get reset to new Vector2(0, 0); on every fixed update.

Another thingBut the script should work as a proper absolute movement controller if you might bedo not have any other scripts or physics effects which affect the velocity of the object.

The first script hits at a misunderstanding isof what Input.GetKeyDown does. When the user is holding down the key, then this method will only be true on single time - during the first frame where the user pressed the key. If you want to do something every frame while the user is holding the key, use Input.GetKey.

Regarding the question in the title "What is best for 2D movement: velocity or AddForce?": It depends on what you want to do.

  • AddForce takes the mass of the object into account. So if you want the player's mass to change and accordingly make the player "feel" lighter or heavier to control, then you should use AddForce. If you do not care about the player's mass, then it might be more convenient to deal with velocity.
  • When you set the velocity to a new value, you cancel all forces which act on the object. Sometimes, this can be intentionalis exactly what you want. But often it is not, because you also cancel any forces which got applied from the physics system or from other scripts. So you usually want to add a new vector to the existing velocity vector, not replace it with a new one.

By default, rigidbodies in Unity behave according to Newton's First Law of Motion:

An object continues to move at a constant velocity, unless acted upon by a force.

So when you add a force to an object, then it gains a velocity and retains that velocity until something slows it down.

But this is not the case if you manipulate the velocity directly. Assigning a new Vector2 to your velocity cancels any velocity the object had before. So with your Move2 script, the object will never retain a velocity between frames, because when the user does not do any input, then the velocity will get reset to new Vector2(0, 0); on every fixed update.

Another thing you might be misunderstanding is what Input.GetKeyDown does. When the user is holding down the key, then this method will only be true on single time - during the first frame where the user pressed the key. If you want to do something every frame while the user is holding the key, use Input.GetKey.

Regarding the question in the title "What is best for 2D movement: velocity or AddForce?": It depends on what you want to do.

  • AddForce takes the mass of the object into account. So if you want the player's mass to change and accordingly make the player "feel" lighter or heavier to control, then you should use AddForce. If you do not care about the player's mass, then it might be more convenient to deal with velocity.
  • When you set the velocity to a new value, you cancel all forces which act on the object. Sometimes, this can be intentional. But often it is not, because you also cancel any forces which got applied from the physics system or from other scripts. So you usually want to add a new vector to the existing velocity vector, not replace it with a new one.

By default, rigidbodies in Unity behave according to Newton's First Law of Motion:

An object continues to move at a constant velocity, unless acted upon by a force.

So when you add a force to an object, then it gains a velocity and retains that velocity until something slows it down.

But this is not the case if you manipulate the velocity directly. Assigning a new Vector2 to your velocity cancels any velocity the object had before. So with your Move2 script, the object will never retain a velocity between frames, because when the user does not do any input, then the velocity will get reset to new Vector2(0, 0); on every fixed update.

But the script should work as a proper absolute movement controller if you do not have any other scripts or physics effects which affect the velocity of the object.

The first script hits at a misunderstanding of what Input.GetKeyDown does. When the user is holding down the key, then this method will only be true on single time - during the first frame where the user pressed the key. If you want to do something every frame while the user is holding the key, use Input.GetKey.

Regarding the question in the title "What is best for 2D movement: velocity or AddForce?": It depends on what you want to do.

  • AddForce takes the mass of the object into account. So if you want the player's mass to change and accordingly make the player "feel" lighter or heavier to control, then you should use AddForce. If you do not care about the player's mass, then it might be more convenient to deal with velocity.
  • When you set the velocity to a new value, you cancel all forces which act on the object. Sometimes this is exactly what you want. But often it is not, because you also cancel any forces which got applied from the physics system or from other scripts. So you usually want to add a new vector to the existing velocity vector, not replace it with a new one.
2 added 22 characters in body
source | link

By default, rigidbodies in Unity behave according to Newton's First Law of Motion:

An object continues to move at a constant velocity, unless acted upon by a force.

So when you add a force to an object, then it gains a velocity and retains that velocity until something slows it down.

But this is not the case if you manipulate the velocity directly. Assigning a new Vector2 to your velocity cancels any velocity the object had before. So with your Move2 script, the object will never retain a velocity between frames, because when the user does not do any input, then the velocity will get reset to new Vector2(0, 0); on every fixed update.

Another thing you might be misunderstanding is what Input.GetKeyDown does. When the user is holding down the key, then this method will only be true on single time - during the first frame where the user pressed the key. If you want to do something every frame while the user is holding the key, use Input.GetKey.

Regarding the question in the title "Whats"What is best for 2D movement Velocity: velocity or addForceAddForce?": It depends on what you want to do.

  • AddForce takes the mass of the object into account. So if you want the player's mass to change and accordingly make the player "feel" lighter or heavier to control, then you should use AddForce. If you do not care about the player's mass, then it might be more convenient to deal with velocity.
  • When you set the velocity to a new value, you cancel all forces which act on the object. Sometimes, this can be intentional. But often it is not, because you also cancel any forces which got applied from the physics system or from other scripts. So you usually want to add a new vector to the existing velocity vector, not replace it with a new one.

By default, rigidbodies in Unity behave according to Newton's First Law of Motion:

An object continues to move at a constant velocity, unless acted upon by a force.

So when you add a force to an object, then it gains a velocity and retains that velocity until something slows it down.

But this is not the case if you manipulate the velocity directly. Assigning a new Vector2 to your velocity cancels any velocity the object had before. So with your Move2 script, the object will never retain a velocity between frames, because when the user does not do any input, then the velocity will get reset to new Vector2(0, 0); on every fixed update.

Another thing you might be misunderstanding is what Input.GetKeyDown does. When the user is holding down the key, then this method will only be true on single time - during the first frame where the user pressed the key. If you want to do something every frame while the user is holding the key, use Input.GetKey.

Regarding the question in the title "Whats is best for 2D movement Velocity or addForce?": It depends on what you want to do.

  • AddForce takes the mass of the object into account. So if you want the player's mass to change and accordingly make the player "feel" lighter or heavier to control, then you should use AddForce. If you do not care about the player's mass, then it might be more convenient to deal with velocity.
  • When you set the velocity to a new value, you cancel all forces which act on the object. Sometimes, this can be intentional. But often it is not, because you also cancel any forces which got applied from the physics system or from other scripts. So you usually want to add to the velocity vector, not replace it with a new one.

By default, rigidbodies in Unity behave according to Newton's First Law of Motion:

An object continues to move at a constant velocity, unless acted upon by a force.

So when you add a force to an object, then it gains a velocity and retains that velocity until something slows it down.

But this is not the case if you manipulate the velocity directly. Assigning a new Vector2 to your velocity cancels any velocity the object had before. So with your Move2 script, the object will never retain a velocity between frames, because when the user does not do any input, then the velocity will get reset to new Vector2(0, 0); on every fixed update.

Another thing you might be misunderstanding is what Input.GetKeyDown does. When the user is holding down the key, then this method will only be true on single time - during the first frame where the user pressed the key. If you want to do something every frame while the user is holding the key, use Input.GetKey.

Regarding the question in the title "What is best for 2D movement: velocity or AddForce?": It depends on what you want to do.

  • AddForce takes the mass of the object into account. So if you want the player's mass to change and accordingly make the player "feel" lighter or heavier to control, then you should use AddForce. If you do not care about the player's mass, then it might be more convenient to deal with velocity.
  • When you set the velocity to a new value, you cancel all forces which act on the object. Sometimes, this can be intentional. But often it is not, because you also cancel any forces which got applied from the physics system or from other scripts. So you usually want to add a new vector to the existing velocity vector, not replace it with a new one.
1
source | link

By default, rigidbodies in Unity behave according to Newton's First Law of Motion:

An object continues to move at a constant velocity, unless acted upon by a force.

So when you add a force to an object, then it gains a velocity and retains that velocity until something slows it down.

But this is not the case if you manipulate the velocity directly. Assigning a new Vector2 to your velocity cancels any velocity the object had before. So with your Move2 script, the object will never retain a velocity between frames, because when the user does not do any input, then the velocity will get reset to new Vector2(0, 0); on every fixed update.

Another thing you might be misunderstanding is what Input.GetKeyDown does. When the user is holding down the key, then this method will only be true on single time - during the first frame where the user pressed the key. If you want to do something every frame while the user is holding the key, use Input.GetKey.

Regarding the question in the title "Whats is best for 2D movement Velocity or addForce?": It depends on what you want to do.

  • AddForce takes the mass of the object into account. So if you want the player's mass to change and accordingly make the player "feel" lighter or heavier to control, then you should use AddForce. If you do not care about the player's mass, then it might be more convenient to deal with velocity.
  • When you set the velocity to a new value, you cancel all forces which act on the object. Sometimes, this can be intentional. But often it is not, because you also cancel any forces which got applied from the physics system or from other scripts. So you usually want to add to the velocity vector, not replace it with a new one.