Search Unity

Resolved Slow rotating object stops randomly

Discussion in 'Physics' started by Jakermake, Apr 29, 2023.

  1. Jakermake

    Jakermake

    Joined:
    Nov 10, 2021
    Posts:
    11
    Hi, In a scene I'm showing a bunch of cubes rotating slowly, using a random torque added on each one at the beginning.

    • Gravity = false
    • Drag and Angular drag = 0
    • Positions constrained = XYZ
    • Mass = 1

    Sometimes, one will start rotating and suddenly stop. I think it happens when the rotation force added is too low. But it shouldn't stop if it started moving already under the conditions mentioned, am I wrong?

    The rotation code, not really the issue I guess but just in case:
    Code (CSharp):
    1. float[] _alternate = { -1f, 1f };
    2. obj.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
    3. obj.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
    4. obj.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
    5. obj.GetComponent<Rigidbody>().AddTorque(new Vector3(Random.Range(1f, 2f) * _alternate[Random.Range(0, 2)], Random.Range(1f, 2f) * _alternate[Random.Range(0, 2)], Random.Range(1f, 2f) * _alternate[Random.Range(0, 2)]) * 0.5f, ForceMode.VelocityChange);
    Might look dumb but it was the only way I found to make torque "non zero" in any axes using negative values as well.


    PD: Offtopic, is there a way to do
    obj.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation -> NONE;

    I'm doing some stupid workaround right now.
     
    Last edited: Apr 29, 2023
  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    Jakermake likes this.
  3. Jakermake

    Jakermake

    Joined:
    Nov 10, 2021
    Posts:
    11
    Thank you so much! Exactly what I needed.

    I'm totally blind. I've checked Rigidbody docs many times trying to find the answer and I was about to give up and write a function to re-apply torque if any object stops, pretty dumb workaround.
     
    tjmaul likes this.