Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Rigidbody.AddForce(x, ForceMode.VelocityChange) Vs Rigidbody.velocity

Discussion in 'Physics' started by Charlicopter, Jun 4, 2023.

  1. Charlicopter

    Charlicopter

    Joined:
    May 15, 2017
    Posts:
    125
    I just upgraded from 2021.3 to Unity 2022.3 and it started throwing warnings about setting Rigidbody.velocity directly as not being supported. My game has worked just fine up til now, and continues to. But these warnings started popping up after the update.

    I use Rigidbody.velocity = X to bring Rigidbodies to a dead-stop, or to instantly change their velocity.

    After changing all instances of Rigidbody.velocity = X to Rigidbody.AddForce(X, ForceMode.VelocityChange), All my physics behavior is completely broken.

    How do I replicate Rigidbody.velocity = X in a "legal" way?
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    The "legal" equivalent to
    rigidbody.velocity = v
    is:
    Code (CSharp):
    1. rigidbody.AddForce(v - rigidbody.velocity, ForceMode.VelocityChange);
    Basically you add the velocity difference with the current velocity so the result is the wanted velocity.
     
    Last edited: Jun 4, 2023
    Charlicopter likes this.