Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    73
    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:
    11,129
    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:
    741
    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:
    46
    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?
     
  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:
    240
    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
     
    fulgorr and arkano22 like this.
  7. IndieMarc

    IndieMarc

    Joined:
    Jan 16, 2017
    Posts:
    183
    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.
     
  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.
     
  9. Liam2349

    Liam2349

    Joined:
    Mar 10, 2016
    Posts:
    78
    Just noticed this whilst testing my multiplayer in 2022 LTS. I switch objects between kinematic and non-kinematic constantly. They will be in different states on different machines.

    It was used as a data container but I also expected set values to be used as the initial values when changing to non-kinematic. Now I have to figure out the extent of the issues this presents.
     
    Malbers and MarcoZVincenzi like this.
  10. PVisser

    PVisser

    Joined:
    Apr 24, 2014
    Posts:
    61
    I ran into the same problem today after installing Unity again after a hiatus and going back to old projects. For some reason the default(?) third person controller gives an error whenever I jump, the same error comes from some moving objects I spawn having velocity.

    Here's some of the code that seems to be the problem.

    Code (CSharp):
    1. public void OnAnimatorMove()
    2.         {
    3.             // we implement this function to override the default root motion.
    4.             // this allows us to modify the positional speed before it's applied.
    5.             if (m_IsGrounded && Time.deltaTime > 0)
    6.             {
    7.                 Vector3 v = (m_Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime;
    8.  
    9.              
    10.  
    11.                 // we preserve the existing y part of the current velocity.
    12.                 v.y = m_Rigidbody.velocity.y;
    13.                 m_Rigidbody.velocity = v;
    14.             }
    15.         }
    How would I change this out to work without errors?
     
    Last edited: Aug 26, 2023
    fulgorr likes this.
  11. leeprobert

    leeprobert

    Joined:
    Feb 12, 2015
    Posts:
    44
    @PVisser - I am trying to do the exact same thing. To get rid of the error you would do something like this:
    ```
    public void OnAnimatorMove()
    {
    if (m_IsGrounded && Time.deltaTime > 0)
    {
    Vector3 v = (m_Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime;​

    // we preserve the existing y part of the current velocity.
    v.y = m_Rigidbody.velocity.y;
    // m_Rigidbody.velocity = v; <--- OLD
    m_Rigidbody.MovePosition(m_Rigidbody.position + v); <--- NEW
    }​
    }
    ```
    This did rapidly increase the speed of the character though
     
    Malbers likes this.
  12. kangaroop

    kangaroop

    Joined:
    Jan 16, 2024
    Posts:
    1
    having the same problem with the topdown engine asset... how can i solve that for evrything at once? ther's too many of them in old projects
     
  13. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,476
    In Visual Studio you can do search and replace (CTRL+H), though review each one rather than doing it in bulk because it could be a bit dangerous. You can also look up all references with shift+F12, which should give you a list of where you're setting that property. And if the projects are sufficiently old, just ignore it :)