Search Unity

OnCollisionStay and keeping a RigidBody awake

Discussion in 'Physics' started by TechSquid, Jan 19, 2020.

  1. TechSquid

    TechSquid

    Joined:
    Jan 12, 2020
    Posts:
    3
    Hello all,

    New to Unity and mostly new to C# with some previous programming experience. I am playing around with Unity and experimenting around with adding my own additional physics. An issue I am running into is I have noticed that `OnCollisionStay` stops firing after a certain amount of time has passed where they are no longer moving.

    I consider my code to be physics-based and I am exchanging information through the two object when they collide and I am using `Time.deltaTime` to moderate the speed of that data transfer.

    The problem is, after the Rigidbody freezes due to not moving, my calculations in `OnCollisionStay` get corrupted because the value of `Time.deltaTime` becomes `infinity`.

    So currently it seems there is some kind of loop in the Rigidbody component that looks to see if there is a minimum amount of "force" being applied before automatically falling asleep.

    Is there some kind of pseudo force I can apply until I am ready for the object to fall asleep?

    Shame the RigidBody code isnt accessible/editable.
     
  2. TechSquid

    TechSquid

    Joined:
    Jan 12, 2020
    Posts:
    3
    Ok I'm still looking into it but the rest may be an error in my unrelated functions perhaps. I believe this is working.


    Code (CSharp):
    1.  void OnCollisionStay(Collision collisionInfo)
    2.     {
    3.         float dataTransferRate = someDataAmount * Time.deltaTime;
    4.         Debug.Log("dataTransferRate (per frame): " + dataTransferRate);
    5.         AddEnergy(dataTransferRate);
    6.        
    7.         if (bool)
    8.         {
    9.             rb.sleepThreshold = 0f;
    10.         } else
    11.         {
    12.             rb.sleepThreshold = 0.005f;
    13.         }
    14.     }