Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Problems with rigidbody collisions in the Webgl build

Discussion in 'Physics' started by ndruha, May 10, 2021.

  1. ndruha

    ndruha

    Joined:
    Jun 5, 2015
    Posts:
    7
    Hello,

    I have issues with the rigibodies that use box colliders in the WebGL build. Sometimes the collisions are simply skipped and they penetrate through each other. The issue happens only in the build, but never happens in Unity Editor.

    Here is the video, and below explanation what's happening:


    The rigidbody 1 (the red box) is being moved normally, using AddForce in FixedUpdate. Force type can be Velocity or Force, reproduced with both. The rigidbody 2 (the blue box) is staying. When rigidbody 1 collides with rigidbody 2, it passes sometimes through, without colliding.

    When this happens for the first time, it starts passing through more often (sometimes all the time) when trying again. It happens under framerate of 60 fps, so the performance is not the issue.

    The collision detection is set to Continious Speculative. It happens even when the sleeping of the rigidbody 2 is permanently disabled by setting sleepTreshold to 0. It also happens when rigidbody 1 is moving very slow, so it is clearly not a speed issue.

    I reproduce this consistently with Unity 2020.3.7f1 and earlier 2020.3 versions.

    Have anybody faced this issue, and does there exist any workaround? So far I tried to play with all the different physics settings, but nothing helps.
     
    Last edited: May 12, 2021
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,428
    Looks like a bug to me, and you have a pretty clear repro scene. I'd file a bug report with it.
     
  3. ndruha

    ndruha

    Joined:
    Jun 5, 2015
    Posts:
    7
    I have already reported a bug with the demo project atrached. But maybe somebody faced it and knows a workaround before they fix it.
     
  4. ndruha

    ndruha

    Joined:
    Jun 5, 2015
    Posts:
    7
    I have found how to work around the issue for now by adding a script to the moving rigidbodies, that uses raycast to detect collisions. I have taken inspiration from here and slightly modified it so it actually works for this case. It does not always look the best but is acceptable workaround. If anybody has this issue, here is the code:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Linq;
    4.  
    5. public class DontGoThroughThings : MonoBehaviour
    6. {
    7.     public LayerMask layerMask = -1;
    8.     public float skinWidth = 0.1f;
    9.  
    10.     public float searchCollisionVerticalOffset = 0.5f;
    11.     private float minimumExtent;
    12.     private float partialExtent;
    13.     private Vector3 previousPosition;
    14.     private Rigidbody myRigidbody;
    15.     private Collider myCollider;
    16.    
    17.     void Start()
    18.     {
    19.         myRigidbody = GetComponent<Rigidbody>();
    20.         myCollider = GetComponent<Collider>();
    21.         previousPosition = myRigidbody.position;
    22.         minimumExtent = Mathf.Min(Mathf.Min(myCollider.bounds.extents.x, myCollider.bounds.extents.y), myCollider.bounds.extents.z);
    23.            partialExtent = minimumExtent * (1.0f - skinWidth);
    24.     }
    25.  
    26.     void FixedUpdate()
    27.     {
    28.         if (myRigidbody.velocity.sqrMagnitude < 0.0001f) {
    29.             return;
    30.         }
    31.  
    32.         Vector3 movementThisStep = myRigidbody.position - previousPosition;
    33.         float movementSqrMagnitude = movementThisStep.sqrMagnitude;
    34.      
    35.         if (movementSqrMagnitude > 0)
    36.         {
    37.             float movementMagnitude = Mathf.Sqrt(movementSqrMagnitude);
    38.             var isObstacle = FindObstacle(previousPosition + Vector3.up * searchCollisionVerticalOffset, movementThisStep, movementMagnitude, layerMask.value, out var hitInfo);
    39.  
    40.             if (isObstacle)
    41.             {
    42.                 if (hitInfo.collider.isTrigger) {
    43.                     hitInfo.collider.SendMessage("OnTriggerEnter", myCollider);
    44.                 }
    45.                 else {
    46.                     myRigidbody.position = hitInfo.point - Vector3.up * searchCollisionVerticalOffset - (movementThisStep / movementMagnitude) * partialExtent;
    47.                 }
    48.             }
    49.         }
    50.         previousPosition = myRigidbody.position;
    51.     }
    52.  
    53.     private bool FindObstacle(Vector3 position, Vector3 step, float distance, int layers, out RaycastHit hitInfo) {
    54.         var hits = Physics.RaycastAll(position, step, distance, layers);
    55.         foreach (var hit in hits) {
    56.             if (hit.collider != myCollider) {
    57.                 hitInfo = hit;
    58.                 return true;
    59.             }
    60.         }
    61.         hitInfo = default(RaycastHit);
    62.         return false;
    63.     }
    64. }
    65.  
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    I'm not a 3D physics dev so unfortunately I cannot advise much but it might be useful to others encountering similar issues if you post the public link to your bug case.
     
  6. ndruha

    ndruha

    Joined:
    Jun 5, 2015
    Posts:
    7
    I don't have the public link to my bug case. I have reported the bug through Unity Editor, and all I got is a private link of the inquiry, that it says I'm not supposed to share. The inquiry id number, if this helps you to find it in the system is: 1334972
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    I've just checked and that case (1334972) is live and in triage status which means it will eventually be processed by QA. They'll try to duplicate the issue and if found they'll accept it and send it onto the relevant team. At which point you'll get an email with a public link where you can track it.

    I added a link to this forum post to it too.
     
    ndruha likes this.
  8. Wordsonplay

    Wordsonplay

    Joined:
    Apr 29, 2013
    Posts:
    21
    What is the status of this bug? I can't find it in the public issue tracker.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    I'll reply but again, know that I have no involvement in this apart from this thread. So the public link is here. I took a look at the case report and can see comments on it, even from yesterday, so it looks like it's active and being investigated and it looks like it's being a tricky issue that's proving difficult to narrow down given the comments.

    Hope that helps.
     
    Edy likes this.
  10. Nolex

    Nolex

    Joined:
    Dec 10, 2010
    Posts:
    115
    @MelvMay, @ndruha, Same problem. In my project, some objects fall into the ground.

    I've done a lot of testing and I can say that this is clearly a Unity bug and only appears on webGL.
    I solved this with Physics.Raycast() but would like to do without it, but I don't think the Unity team will fix it.

    Unity 2019.4.37f
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    Please see what I said above when I said:
    There's simply no point tagging me in.

    I presume you looked at the case link? As you can see, the case says 3rd party issue so no, it won't be fixed by Unity from the look of it.

    But again, I have zero involvement or knowledge of this, I'm just being the messenger here. No more tagging please. ;)
     
  12. Nolex

    Nolex

    Joined:
    Dec 10, 2010
    Posts:
    115
    I looked at this link and I think this issue is not well researched.

    As far as I know, the version of the physics engine has not changed in version 2019.4.X, but this problem appeared.
    Although in the previous version of Unity 2019.2.X there was no such problem.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521