# How to keep gravity when adding velocity to Rigidbody

Hey all,

I have a rigidbody on a hill and have it face a random direction. I then want to apply a velocity to the rigidbody, but I can't see how to do this without nulling the effect of gravity. That is, if I set the y-velocity to be zero, the rigidbody just floats through the air. Is there a way to keep gravity and have velocity only in the x- and z-directions?

You should NOT ever modify the velocity directly, use it as read-only.

For anyone else seeing this post after 4 years or later - whenever you AddForce in any direction, you apply exactly **(added_force)/(50rigidbody’s_mass) *** velocity to that direction.

For example: let’s say you have an object with rigidbody, a mass of 3, that stays in one place in air, and is not affected by gravity. You wanted it to start moving in X direction with velocity of 13.37.

So 13.37(503) = 13.37150 = 2005.5** is the amount of force you need to add.

``````var wantedVel : float = 13.37; //we want 13.37 velocity

function Start() {
}

var calculatedForce : float = wantedVel*(50*rigidbody.mass); //calculate force amount

Debug.Log("velocity: " + rigidbody.velocity.x); //debug to see final velocity
}

CONSOLE:
velocity: 13.37
``````

Now, if you need to add angled velocity, not in just one specific direction, then google all about Vector Addition, it may scare you at first, but trust me, it is very easy. Or just search Unity for adding angular force.

I hope I helped someone :D.

So I figured this out and thought I would share it. It appears that whenever you set the velocity for a direction, it negates forces in that direction. I circumvented this by only ever setting the x- and z-components of the velocity vector specifically, and not touching the y-component.

If there is a more elegant way, please let me know!

perhaps when doing the y component, also add in the Physics.gravity amount to the y value?