Search Unity

Bug iOS build physics behave differently on the iphone

Discussion in 'Physics' started by FrostSTeeL, Jan 14, 2023.

  1. FrostSTeeL

    FrostSTeeL

    Joined:
    Apr 4, 2020
    Posts:
    1
    When i play the game in the editor window, everything works as intended. But when i build and play the game on the iPhone, my object bounces off of the screen. I set Physics Materials with 0 bounciness but i think this is something different.

    This is the code that makes the cube jump around:
    Code (CSharp):
    1. IEnumerator JumpAroundDelay()
    2.     {
    3.         while (true)
    4.         {
    5.             switch (direction)
    6.             {
    7.                 case 1:
    8.                     rb.velocity = new Vector2(0, 0);
    9.                     rb.AddForce(new Vector2(10, 10), ForceMode2D.Impulse);
    10.                     direction = 2;
    11.                     yield return new WaitForSeconds(Random.Range(0.1f, top));
    12.                     break;
    13.                 case 2:
    14.                     rb.velocity = new Vector2(0, 0);
    15.                     rb.AddForce(new Vector2(-10, 10), ForceMode2D.Impulse);
    16.                     direction = 1;
    17.                     yield return new WaitForSeconds(Random.Range(0.1f, top));
    18.                     break;
    19.             }
    20.         }
    21.     }
    This is the Cubes inspector: https://imgur.com/a/iFxb1UR
    This is how it should behave:https://i.stack.imgur.com/6d8yq.gif
    This is what it is doing: https://imgur.com/a/32TEBQl


    As you can see somehow when the cube hits on its straight side on the walls, it bounces off violently. It doesn't do this in the editor or on an Android device. Only on iPhone. Anyone encountered something similar?
     
    arto00 and paljan like this.
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,924
    You're setting the rigidbody velocity and adding a force to it in a coroutine, this will yield largely undefined results. Physics stuff must always be done in FixedUpdate. In this case, you could use yield return new WaitForFixedUpdate(); before you do any physics related stuff in your coroutine and it should work.

    Another potential problem: you probably want to enable the "Freeze Rotation Z" checkbox on your rigidbody. Otherwise it will be free to rotate, which depending on slight contact precision differences will cause it to sometimes rotate even when hitting a flat surface.
     
  3. paljan

    paljan

    Joined:
    Feb 9, 2015
    Posts:
    32
    Would be interested to hear if you found the culprit in your case. We have similiar issues after updating Unity from 2021 to the 2022.2.1 version. We have multiple projects with multiple minigames in them, and for some reason many started to exhibit similiar behaviour.

    Used to work without issues in 2021 and earlier, works fine in editor in 2022.2.1, but when building for iOS and testing on a device, physics2D rigidbodies seem to randomly get a huge impulse when just bouncing around freely. Also not appearing on android builds. Happens when the physics is just being simulated, without any additional specific force added by game logic.
     
    ramsayamarin and arto00 like this.
  4. uncolike

    uncolike

    Joined:
    Jan 17, 2020
    Posts:
    31