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:
    42
    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:
    88
    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. XenoRo

    XenoRo

    Joined:
    Jul 22, 2014
    Posts:
    22
    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:
    1,552
    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).
     
    Ruchir likes this.
  5. Ruchir

    Ruchir

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

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,610
    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:
    88
    AddForce(TargetVelocity - CurrentVelocity,ForceMode.VelocityChange);
     
    Ruchir likes this.
  8. Ruchir

    Ruchir

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