# Does rigidbody .velocity stop gravity from working?

I watched a video which said if you use .velocity to move an object that object no longer is effected by gravity.

I think I have this problem. Ive been tinkering with a wingsuit/glider like system, most of it doesn’t work, but for the most part im gliding around using .velocity. The issue is once I start gliding, gravity stops caring about me even when im not gliding.

Is there an alternative?

``````            rb.drag = mod_drag;
Vector3 localV = transform.InverseTransformDirection(rb.velocity);
localV.z = mod_speed;
rb.velocity = transform.TransformDirection(localV);
``````

I don’t know if it is not affected by gravity, but the problem is that if you set it’s velocity than you overwrite the y velocity component that was accumulated by the acceleration of gravity.

So if you insist on setting velocity than you will have to store y component set the velocity and restore the y component of velocity.

But what you should do is to not set Velocity directly but learn how to use AddForce

Technically speaking, no. The object will still be affected by gravity. Gravity is not a velocity, it’s an acceleration. Under the hood, gravity is applied somewhat like, if not exactly like, `subjectRigidbody.velocity = subjectRigidbody.velocity + gravity * Time.fixedDeltaTime`, every physics frame.

However, if you set a Rigidbody’s velocity in a way that doesn’t inherit the previously existing velocity like in the code above, you will override it, including any velocity accquired from gravity’s acceleration. This will lead to objects not being able to acquire more than 1 frame’s worth of gravity acceleration.

Just use Rigidbody.AddForce(velocityVector, ForceMode.VelocityChange) instead of modifying Rigidbody.velocity directly. This will sum the change in velocity to the value already calculated internally based on the other forces and accelerations (gravity, impacts, etc).

2 Likes

Can you please explain it once I didn’t get what you were saying:eyes:

He wrote it perfectly clearly, you need to use AddForce not .velocity.
Make sure the ForceMode parameter is set to VelocityChange.

1 Like

2 Likes

So will this prevent the one frame acceleratioon due to collison or gravity (Using this method)?

Problem is the player starts accelerating and not move at the speed set.with addforce

1 Like

Hi! Sorry to add to an old post, but I have a similar gliding problem in my game. I had previously been hard-coding velocity which was naturally overriding the pull of gravity, but since replacing it with AddForce() I still have the same gravity-ignoring results (albeit cleaner code). Am I missing something?

``````private void setVelocity()
{
Vector3 vel = transform.forward * glidingSpeed;
body.AddForce( vel - body.velocity , ForceMode.VelocityChange );
}
``````

I haven’t tested this, but what about preserving the current vertical velocity? Something like:

``````Vector3 vel = transform.forward * gliddingSpeed;
vel.y = body.velocity.y;
``````
1 Like

I’ve got a whole bunch more problems now, but those are to do with my expectations rather than any physics bug haha

2 Likes

also, using addforce causes the player to bounce and slide around with moon physics on an ice planet.

seems the solution is what is mentioned above, which is to store and Y value and reapply it instead of 0.

edit: btw, produces worse results than addforce.

Oh well, it seems I missed something among updates. As far a I knew there were only two settings: Force and Impulse. Thanks.

ForceMode2D has two modes Force And Impulse, which is a part of rigidbody2D You will find other options in ForceMode which will come with rigidboody.(3D component)

1 Like