# What is best for 2D movement: velocity or AddForce?

So I have tried both Velocity and addForce but I'm not getting the result I was expecting which makes me wonder if I fully understand what they do.

Move script (test script 1)

void Update () {

if(Input.GetKeyDown(KeyCode.Space)){
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
}

if(Input.GetKeyDown(KeyCode.A)){
rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
}

if (Input.GetKeyDown(KeyCode.D)){
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
}
}


Move2 script (test script 2)

void FixedUpdate() {
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");

rbody.velocity = new Vector2(x*speed, y*speed);
}


Both scripts are used independently.

Velocity when I press the key down (D or A) it stops after short time but addForce seems to do the same while that is set in FixedUpdate() or Update(), which is confusing as I thought it would keep it moving till the key was released? I have also found that if I press D it moves then stops but if I release D and press again and repeat the player does not move until I press a different move direction. Any ideas?

• From the title of your question, it sounds like you might be looking for "When should I use velocity versus AddForce?" But the body of your question looks like your trouble might be in either choosing how to respond to input or in combining movement effects. Can you clarify whether you're trying to use these two scripts together or one object, or are they on two different objects and neither one is doing what you want? – DMGregory Sep 20 '18 at 12:19
• Sorry wasn't very clear. I am using each method independently of each other to see the results but also switching them from Update to FixedUpdate. – user225976 Sep 20 '18 at 13:01

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.
• Now I understand that GetKeyDown issues one iteration of the command but when I use GetKey this also stops and when released and pressed again does not move. example: I press A to move left I release after the player stops and press the same A button again but nothing happens. I am also seeing an issue with the movement now that it stops at different distances any reason why that would happen? – user225976 Sep 20 '18 at 13:19
• @user225976 I am not sure why this would happen. It feels like you are not showing us the whole picture. As if there was more code in your project which affects the velocity of your player. – Philipp Sep 20 '18 at 13:33
• Philipp - I have marked my question answered as our ongoing conversation is moving away from that. I have opened a new question regarding the issue. gamedev.stackexchange.com/questions/163859/2d-movement-sporadic – user225976 Sep 20 '18 at 14:28