Search Unity

Setting rigidbody.angularVelocity **sometimes** freezes my game!

Discussion in 'Editor & General Support' started by adamgryu, Jul 31, 2019.

  1. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    187
    UPDATE: I edited the title to reflect my discovery, but I still want to know why this happens since it might affect other areas of my code.

    Hi! I'm having a weird problem with my game.

    In my game, you can cast a fishing rod. It seems like in super rare cases, casting the fishing rod freezes the game for a long time, if not indefinitely (it's very strange that sometimes it eventually regains control after several minutes!)

    I've checked the fishing rod code and I don't see where this infinite loop / recursion could be coming from.
    When I pause the VS debugger during this freeze, it says "No compatible code detected."

    I tried following steps from this thread (https://forum.unity.com/threads/usererror-how-to-find-your-infinite-loop.378384/) to locate the issue in the native Unity code, but I couldn't figure out how to make this information useful:
    upload_2019-7-30_21-48-41.png

    Also, because I still can't consistently reproduce the freeze, it's impossible to solve the error via trial-and-error. It seems to happen completely randomly over different time intervals. (I've spent several hours just trying different ways to reproduce the bug).

    I checked the Editor logs and there's nothing strange happening in them.

    I'm at a loss here about how to proceed. Does anyone have any advice?
     
    Last edited: Apr 16, 2020
  2. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    187
    Actually, I dug up this profiler screenshot from a month ago, when I first saw the issue. (It's so rare I hadn't seen it for a whole month!)



    I think this profiler frame is from a lag spike that eventually ended. Some of the ones I've been seeing lately seemed like they would never end and I had to close the editor.

    Is there some sort of deadlock going on behind the native code here?
     
  3. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    187
    Hey, just in case anyone else had the problem, I fixed it by replacing:

    body.angularVelocity = Random.insideSphere;

    to

    body.ApplyTorque(Random.insideSphere);

    For some reason setting the velocity directly would crash my game like, every 1000th time.
     
    fffMalzbier likes this.
  4. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    187
    I'm worried I might be seeing this issue again but in a different context.

    This time, I'm not using body.angularVelocity anywhere, after changing some different physics code, I'm getting rare occasional freezes. I can't reproduce this consistently yet, so I can't figure out what's causing it. (I was only able to figure out the previous workaround by automating part of my game to reproduce the bug).

    The previous issue WAS resolved with my workaround, but I never figured out what the real root of the issue was. It seems like a bug in the Unity physics engine. I was wondering if someone on the Unity physics team could provide some insight into why my previous workaround worked, and if this bug has been fixed in a future patch? (I'm on Unity 2018.4.2)
     
    Last edited: Apr 16, 2020