# How to calculate the post Rigidbody2D velocity vector before applying a specific force in Unity?

I am trying to implement a space rocket in 2D. Whenever the player presses a button, a constant force (Both vertical and horizontal) is being added to that rocket. What I want to add is a maximum speed, the rocket cant exceed as the rocket will continue accelerating to infinity. The player can also choose not to accelerate, which can lead to the rocket starting to fall down due to gravity. Thus, I cant just Clamp the rigidbody.velocity of the rocket whenever the magnitude of it exceeds max speed as it will also limit the fall velocity which is not as limited. Thus, I am trying to predict whether the player will exceed the maximum velocity when trying to accelerate, in order not to add the force.

So far, I have tried doing this but the velocity vector does not seem to change immediately, so vel is equal to postVel:

Vector2 vel = rigidbody.velocity;
Vector2 postVel = rigidbody.velocity;

• Note that you might get more helpful answers if you would tell us what you want to accomplish by doing this. Feb 3, 2020 at 17:00

Remmber: F=MA

When you add force you are modifying the acceleration, which will in turn change velocity as time progresses.

You have added and subtracted force in the same instant, without allowing time for the newly changed rate of acceleration to modify the velocity. Add some brief sleeps in your code and you will see the change in velocity.

• The brief sleeps won't work because the prediction has to be instant Feb 3, 2020 at 19:15
• I think you are fundamentally misunderstanding the basis of real world physics which the game engine is attempting to simulate. Regardless of the magnitude of the change in force, there will be no change in velocity until there has been a change in time. Feb 3, 2020 at 19:17
• @PanagiotisIatrou I think this may be an X/Y problem. If you provide some more specifics about your situation and what you are trying to accomplish I think someone will be able to provide a more specific answer. It is hard to understand what exactly the inputs and outputs you are looking for are, and what your current implementation is. Feb 3, 2020 at 19:22
• I edited the question body Feb 3, 2020 at 19:35

Adding forces to a rigidbody won't change its velocity until the phyiscs engine runs another update, which happens betweeen two calls to a FixedUpdate method in your own code.

But if you want to predict the velocity of a body after applying a force, you can do so using Newton's formula force = mass * acceleration, or acceleration = force / mass.

Vector3 newVelocity = rigidbody.velocity + Force / rigidbody.mass;

But keep in mind that drag, other forces and collisions with other bodies might cause the velocity on the next frame to be different than you predicted.

• if force / rigidbody.mass is equal to acceleration then why do we add that to the new velocity? Shouldn't we add a velocity, instead of acceleration? This piece of code is on FixedUpdate(): Vector2 vel = rb.velocity; Vector2 newVel = vel + (force / rb.mass); Debug.Log(vel.magnitude + " vs " + newVel.magnitude); rb.AddRelativeForce(force); And it prints this which is not the result wanted. What I want is to predict the next "vel" Feb 3, 2020 at 18:52