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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Physics behave differently in WebGL than other builds

Discussion in 'Physics' started by Tunztunztunz, Dec 24, 2021.

  1. Tunztunztunz

    Tunztunztunz

    Joined:
    Jan 7, 2016
    Posts:
    8
    I've run into a problem with my small Unity project. It behaves completely differently when I build it in WebGL. The force I apply works and sends the ball in my scene where I want it to go, however, when I build and run in WebGL the ball seems to get propelled at randomly hard and softer forces than what I see in editor and on a Windows build. I have all the physics work inside of fixedUpdate() and I'm altering state when OnCollisionEnter() is triggered. I can't find anyone else who actually has this exact issue, so I decided to try here.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Boost : MonoBehaviour
    7. {
    8.     Rigidbody rb;
    9.     bool collided = false;
    10.     int boosted = 0;
    11.     Vector3 dir;
    12.     string message = "Booster Hit";
    13.  
    14.     private void Awake()
    15.     {
    16.         rb = GetComponent<Rigidbody>();
    17.     }
    18.  
    19.     private void FixedUpdate()
    20.     {
    21.         if (collided) {
    22.             float forceAmount = 155;
    23.  
    24.             if (boosted > 1)
    25.             {
    26.                 Debug.Log("Third Boost");
    27.                 forceAmount = 104;
    28.             }
    29.             else if (boosted > 0)
    30.             {
    31.                 Debug.Log("Second Boost");
    32.                 forceAmount = 160;
    33.             }
    34.  
    35.             rb.AddForce(dir * forceAmount, ForceMode.Impulse);
    36.             boosted++;
    37.             collided = false;
    38.         }
    39.     }
    40.  
    41.     private void OnCollisionEnter(Collision collision)
    42.     {
    43.         if (collision.gameObject.tag == "Booster")
    44.         {
    45.             Debug.Log(message);
    46.             dir = collision.GetContact(0).normal;
    47.             collided = true;
    48.         }
    49.     }
    50. }
    51.  
    This is the only custom script in the scene. It just propels the Ball object in the opposite direction of the Booster objects when they make contact on the track. It almost seems as if I'm running an entirely different physics engine when I build/run WebGL.

    Example Videos:
    Inside Editor -

    WebGL Build -
     
    Last edited: Dec 24, 2021
  2. jameso2

    jameso2

    Joined:
    Apr 26, 2020
    Posts:
    3
    Hi, I'm new to Unity and just encountered this same issue.

    My "game" is much simpler. I was following along the learn.unity.com tutorial lessons. There's a part where they ask you to make a scene with a ball rolling down some path. My scene is literally just a Sphere rolling down 3 elongated cubes. There's no extra scripts or anything special -- its just a new 3D Project with a few primatives with some basic out-of-the-box physics applied. It should roll down in the exact same path every time the app is run.

    But in the WebGL build, the ball ends up getting stuck in a spot that doesn't happen in the Windows Build or playing it in the Editor.

    This leads me to believe that the physics logic for the WebGL build is just completely different. I took screenshots to compare:


    The top image shows what happens on WebGL build, the bottom shows how it behaves in Windows Build and the Editor play preview. The ball should just fall on the planks (blue line) and roll all the way down the green block. But on WebGL it gets stuck half way and stops rolling (circled in red).
     
    DonPuno likes this.
  3. Tunztunztunz

    Tunztunztunz

    Joined:
    Jan 7, 2016
    Posts:
    8
    I did an experiment and used a unity sphere using the sphere collider and it behaves the same in the webgl build as it does in the editor. This might be a bug with the mesh collider that ProBuilder uses or I'm using it incorrect? Either way, the only time I can get this ball's physics to behave consistently is when it's created by creating a sphere object in unity.
     
  4. jameso2

    jameso2

    Joined:
    Apr 26, 2020
    Posts:
    3
    I didn't use ProBuilder. I was just placing objects from primitives.

    I mean, the physics in the Editor/other builds versus WebGL are incredibly similar, so 99% of the time you probably won't notice a difference.

    In the example I showed above, in a normal build the ball does stop at the same point where I circled red in the WebGL version. But the dark wooden "plank" shape is slightly rotated to the right (like 1 degree), which should cause the ball to SLOWLY roll off the right side and eventually down to the lower part of the track.

    In the WebGL version, this never happens (the ball won't roll down right side of the plank), which means the ball/engine is ignoring this tiny slope (not following physics how you would assume it should).

    My guess is that this bug is due to a rounding error, because the WebGL version is treating this very low 1 degree slope as if its entirely flat.

    I'm gonna try to recreate the bug, and see if I can isolate the issue. But overall, the fact that the physics behaves differently in different builds makes it unreliable to use for anything serious.
     
    Last edited: Dec 30, 2021
  5. Ollag1999

    Ollag1999

    Joined:
    Feb 28, 2023
    Posts:
    3
    @jameso2 I'm having a similar issue on the same Unity Learn challenge. It's disappointing to see this post is from Dec 2021 and there doesn't seem to be any real solution presented. My issue is that my sphere bounces in a completely different direction in the uploaded WebGL build than in Editor and Game Mode. I posted my issue in a forum and only got one reply suggesting to have a loading screen or a small script to create a 2-5 frame delay before the game starts. I don't know enough to do either of these solutions, but I'm trying g to figure it out. I believe I have a working loading screen, but now the lighting of my scene loads incorrectly, causing extremely dark shadowing on everything.