Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. 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:
    122
    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,414
    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.