- Home /

# simulate gravity in a 2D game

Is there a way to apply acceleration directly to RigidBody2D.velocity? If not, is there any way to simulate gravity as a gameobject's velocity? I am really new to Unity, and I am making a 2D game where you can use direction keys to change the gravity directions. Since the game is multiplayer, using physics2d.gravity might not be applicable.

What part are you stuck at?

I keep a separate position and velocity pair, and pass that into my acceleration function. Then when I'm done adjusting the position and velocity, I call `game_object.transform.position = temp_position;`

-- I do not use the transform.velocity variable.

Here is the Runge-$$anonymous$$utta order 4 integrator:

```
void proceed_rk4(ref tennis_parameters t, ref Vector3 pos, ref Vector3 vel, Vector3 ang_vel, float dt)
{
const float one_sixth = 1.0f / 6.0f;
Vector3 k1_velocity = vel;
Vector3 k1_acceleration = acceleration(ref t, pos, k1_velocity, ang_vel);
Vector3 k2_velocity = vel + k1_acceleration * dt * 0.5f;
Vector3 k2_acceleration = acceleration(ref t, pos + k1_velocity * dt * 0.5f, k2_velocity, ang_vel);
Vector3 k3_velocity = vel + k2_acceleration * dt * 0.5f;
Vector3 k3_acceleration = acceleration(ref t, pos + k2_velocity * dt * 0.5f, k3_velocity, ang_vel);
Vector3 k4_velocity = vel + k3_acceleration * dt;
Vector3 k4_acceleration = acceleration(ref t, pos + k3_velocity * dt, k4_velocity, ang_vel);
vel += (k1_acceleration + (k2_acceleration + k3_acceleration) * 2.0f + k4_acceleration) * one_sixth * dt;
pos += (k1_velocity + (k2_velocity + k3_velocity) * 2.0f + k4_velocity) * one_sixth * dt;
}
```

And here is the acceleration function:

```
Vector3 acceleration(ref tennis_parameters t, Vector3 pos, Vector3 vel, Vector3 ang_vel)
{
// Magnus effect, in metres per second, per second
// http://farside.ph.utexas.edu/teaching/329/lectures/node43.html
// http://spiff.rit.edu/richmond/baseball/traj/traj.html
Vector3 magnus_accel = Vector3.Cross(vel, ang_vel);
magnus_accel *= 0.5f * t.air_density * t.lift_coeff * t.ball_cross_section / t.ball_mass;
// Wind and drag, in metres per second, per second
Vector3 drag_vel = t.wind_velocity - vel;
Vector3 drag_accel = drag_vel * drag_vel.magnitude * 0.5f * t.air_density * t.drag_coeff * t.ball_cross_section / t.ball_mass;
return t.gravitational_acceleration + magnus_accel + drag_accel;
}
```

I forgot to mention:

```
gravitational_acceleration = new Vector3(0, -9.81f, 0);
```

**Answer** by tormentoarmagedoom
·
Feb 18 at 06:50 PM

Hello.

Simulate gravity is not add a velocity to an object. Is to add a force, in this case to the rigidbody component.

https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

This is the correct way to do it, so investigate it and do some tests.

Good luck"!

Thanks for replying, but as I said, I am using RigidBody2D, and using AddForce won't give me an acceleration. Plus, ForceMode2D doesn't have Acceleration as ForceMode

Hello.

Gravity is an acceleration, Acceleration is Force*mass. If you apply a force each frame in a direction, it will do the same as the gravity.

As manual of Addforce says: "Force is applied continuously along the direction of the force vector. Specifying the ForceMode mode allows the type of force to be changed to an Acceleration, Impulse or Velocity Change."

Bye.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.