Search Unity

Does rigidbody .velocity stop gravity from working?

Discussion in 'Physics' started by cinderousrex, Sep 23, 2019.

  1. cinderousrex

    cinderousrex

    Joined:
    May 21, 2019
    Posts:
    43
    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?

    Code (CSharp):
    1.      
    2.             rb.drag = mod_drag;
    3.             Vector3 localV = transform.InverseTransformDirection(rb.velocity);
    4.             localV.z = mod_speed;
    5.             rb.velocity = transform.TransformDirection(localV);
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,074
    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
     
  3. CosmicGiant

    CosmicGiant

    Joined:
    Jul 22, 2014
    Posts:
    23
    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.
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    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).
     
    troyappel and Ruchir like this.
  5. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    Can you please explain it once I didn't get what you were sayingo_O
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    He wrote it perfectly clearly, you need to use AddForce not .velocity.
    Make sure the ForceMode parameter is set to VelocityChange.
     
    Ruchir likes this.
  7. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,074
    AddForce(TargetVelocity - CurrentVelocity,ForceMode.VelocityChange);
     
    Edy and Ruchir like this.
  8. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    So will this prevent the one frame acceleratioon due to collison or gravity (Using this method)?
     
  9. sudhakarjha00007

    sudhakarjha00007

    Joined:
    Mar 25, 2020
    Posts:
    1
    Problem is the player starts accelerating and not move at the speed set.with addforce
     
    nsv_cerberus likes this.
  10. oldManPig

    oldManPig

    Joined:
    Oct 25, 2019
    Posts:
    13
    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?

    Code (CSharp):
    1. private void setVelocity()
    2. {
    3.      Vector3 vel = transform.forward * glidingSpeed;
    4.      body.AddForce( vel - body.velocity , ForceMode.VelocityChange );
    5. }
     
    Last edited by a moderator: Aug 18, 2020
  11. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    I haven't tested this, but what about preserving the current vertical velocity? Something like:
    Code (CSharp):
    1. Vector3 vel = transform.forward * gliddingSpeed;
    2. vel.y = body.velocity.y;
    3. body.AddForce(vel - body.velocity, ForceMode.VelocityChange);
     
    oldManPig likes this.
  12. oldManPig

    oldManPig

    Joined:
    Oct 25, 2019
    Posts:
    13
    Thanks Edy, your answer definitely does what you'd expect.
    I've got a whole bunch more problems now, but those are to do with my expectations rather than any physics bug haha
     
    hippocoder and Edy like this.
  13. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    197

    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.
     
    Last edited: Jan 15, 2021
  14. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    Oh well, it seems I missed something among updates. As far a I knew there were only two settings: Force and Impulse. Thanks.
     
  15. savipokhria125

    savipokhria125

    Joined:
    Mar 12, 2019
    Posts:
    1
    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)
     
    Edy likes this.