Why does Rigidbody.OnDisable() reset the velocity?

If you call gameObject.SetActive(false) on an object with a Rigidbody2D, and later SetActive(true), the object will lose its velocity and angular velocity. This consistently happens to me when there’s a frame delay between deactivating and re-activating the object (won’t work if you immediately call SetActive(true) afterward).

note that changing rigidbody.simulated to false and then true will NOT reset the velocity.

I suspect that this also happens with regular Rigidbody but I only tested this with 2D physics.

This is frustrating, unexpected, unintuitive behavior. Why doesn’t it just freeze the velocities until it is reactivated?

Can I find a workaround that does not require me to cache the velocities with a new component (or in a big table somewhere)?

Frustrating maybe for you, but certainly not unexpected or unintuitive. Think of how much of a bother object pooling would be if you’d have to keep track of physics attributes. If the velocity is not removed, how would Unity keep the object from moving? Or, if it kept moving and OnEnable set it to the original location, how would unity know what location it is without back tracking and recording additional values? The things you want are completely unnecessary, unintuitive and unoptimized.

Setting an object inactive means treating it as non-existent. If it’s non-existent, it doesn’t travel, and if it’s not travelling, it has no velocity.

Meanwhile you, as a programmer, can alter this behaviour. Having rigidbodies tax the game unnecessarily for people who don’t need this kind of behaviour would be blasphemous.

If someone DOES happen to be looking for how to store it in another component and then re add it on enable (this is the first search result), here is something I made for 2D, probably could alter it for 3D as well.

Get the script, then order it just above the wanted Rigidbody2D and it should automatically grab the velocity and store/re set it on enable disable. You can also prefill the Rigidbody2D in the script (helps permformance for prefab spamming)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RigidbodyKeepMomentumOnDisable : MonoBehaviour
{
    public Rigidbody2D thisRB2D;

    Vector2 storedVelocity;
    float storedAngularVelocity;

    //
    // When the RB Object is disabled
    private void OnDisable()
    {
        // get our rigid boy if we didn't prefill
        if (thisRB2D == null)
        {
            thisRB2D = this.GetComponent<Rigidbody2D>();
        }


        // store our velocity and momentum
        storedVelocity = thisRB2D.velocity;
        storedAngularVelocity = thisRB2D.angularVelocity;
    }

    //
    // When the RB object is enabled & when it is first made
    private void OnEnable()
    {
        // get our rigid boy if we didn't prefill
        // might be a good idea to prefill because bullet hell with 200 objects spawning at once vs GetComponent is a bad idea
        if (thisRB2D == null)
        {
            thisRB2D = this.GetComponent<Rigidbody2D>();
        }

        // set velocity and angular velocity, if we have any stored
        if (thisRB2D.velocity != null)
        {
            thisRB2D.velocity = storedVelocity;
        }
        if (thisRB2D.angularVelocity != 0)
        {
            thisRB2D.angularVelocity = storedAngularVelocity;
        }


        // wake up aleady!
        thisRB2D.WakeUp();
    }
}