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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Kinematic rigidbodies no longer holding velocity property?

Discussion in 'Physics' started by gg_michael, Jan 19, 2022.

  1. gg_michael

    gg_michael

    Joined:
    Sep 24, 2012
    Posts:
    60
    An asset I use relies on setting and then reading the velocity property of kinematic rigidbodies. Obviously setting the velocity of a kinematic rb should have no effect on its movement, but this value was used in later calculations to manually translate attached objects.

    Now it seems, as of the latest 2022.1 beta at least, that this no longer works. Directly setting the velocity (or angularVelocity) of a kinematic rb does nothing. It isn't stored. You can try this by manually setting the velocity of a kinematic rb to some non-zero value, then logging the velocity afterwards. No matter what you set it to it will always be 0. This is not the behavior of previous unity versions; previously you could set the velocity of a kinematic rb and it would store that value until the next physics tick.

    Here's a test script illustrating the issue:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. /// <summary>
    4. /// Attach this to a kinematic rigidbody
    5. /// </summary>
    6. public class VelocityExample : MonoBehaviour
    7. {
    8.     void Start()
    9.     {
    10.         Rigidbody rb = GetComponent<Rigidbody>();
    11.         Vector3 setVelocity = new Vector3(1, 2, 3);
    12.         rb.velocity = setVelocity;
    13.        
    14.         // On 2020.3.18f1 this output is equal to setVelocity; on 2022.1.0b3.2528 this output is always (0.00, 0.00, 0.00)
    15.         Debug.Log(rb.velocity);
    16.     }
    17. }
    My question is, was this an intentional change or is this a bug?
     
    ErnestSurys and iDerp69 like this.
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    9,413
    For 2D it's perfectly valid to set the linear/angular velocity of a Rigidbody2D and it'll move like that, it just won't have any external forces applied to it.

    Anyway, I'm not a 3D physics dev but looking at the source here, I see it emits a console warning if you try to set the velocity of "Setting linear velocity of a kinematic body is not supported.". This landed as part of a fix for the following bug report here. It looks like it landed in 2022.1.0a7. The commit was described as "Fix an undefined behaviour arising when setting velocity of a Kinematic body". It'll also be in the release notes with the case number I linked too.

    I would suggest that if you have a problem with it then submit a bug report and potentially link to the report I mentioned here.

    Note again, I'm not a 3D physics dev so I neither understand the reason for the change nor can I do anything about it. It's likely to be perfectly valid if not inconvenient but I'm just not sure.
     
  3. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    688
    Late reply but if we set a RB to kinematic, will it set the linear and angular velocities to zero ? Because if not when we disable kinematic it will continue on with the forces it had before being set to kinematic which is a problem. I currently set linear and angular velocities to zero when setting kinematic on, so that when i set the kinematic off the RB will not suddenly move away from the drop point. But now this method doesn't work so now i am not sure how to reset the data...
     
  4. iDerp69

    iDerp69

    Joined:
    Oct 27, 2018
    Posts:
    37
    I'm using Photon Fusion and Unity 2022.1 and on my client test instance I'm getting spammed in the console every simulation frame:

    Setting linear velocity of a kinematic body is not supported.
    Setting angular velocity of a kinematic body is not supported.​

    This didn't happen in older versions of Unity and is causing significant in-editor performance degradation during testing. Are there plans to fix this or a way to disable this warning?
     
    arkano22, Zaflores and Alima-Studios like this.
  5. dpr-010

    dpr-010

    Joined:
    Jul 5, 2022
    Posts:
    1
    Also having this issue and would love to know if there will be a fix any time soon.
     
  6. kalineh

    kalineh

    Joined:
    Dec 23, 2015
    Posts:
    239
    this log is really annoying and we toggle kinematic on and off all over the place
    please just make it ignore the velocity if kinematic
     
    arkano22 likes this.
  7. Marc477

    Marc477

    Joined:
    Jan 16, 2017
    Posts:
    146
    I don't understand why it would ignore the velocity? This has been working like forever. No need to change the behavior and no need to set a warning. This breaks things for unity assets.

    Setting Rigibody velocity DOES work for kinematic rigidbody and it had always worked before. Why is this being removed? This warning should be removed.
     
    Jouni-Mannonen, arkano22 and iDerp69 like this.
  8. Augustinas-Simkus

    Augustinas-Simkus

    Unity Technologies

    Joined:
    Mar 9, 2020
    Posts:
    84
    Hey, setting velocity for kinematic Rigidbodies is undefined in PhysX. See error here: https://github.com/NVIDIAGameWorks/PhysX/blob/4.1/physx/source/physx/src/NpRigidDynamic.cpp#L230

    We used to allow that accidentally, but the bug report that Melv linked to brought to light the fact that this setter on a kinematic Rigidbody causes undefined behavior, meaning you can get different simulation results setting the exact same velocity in the exact same scenario.

    Unity is shipped with release build of PhysX where these error checks are not present so we added one on our side.
    The best way to control kinematic Rigidbodies is through the Move/MovePosition/MoveRotation methods.
    If your inputs are velocites, you could use Move methods to apply them. The Move methods essentially set the required velocity to reach the target in 1 step like this:
    v = (p_target + p_current) / dt

    So you could set the target to
    p_current + input_velocity * dt
    each step.
     
    jperman8 and MelvMay like this.