Search Unity

Question Physics 2D, the shapes will start shaking, sometimes overlapped.

Discussion in 'Physics' started by uncolike, Apr 21, 2023.

  1. uncolike

    uncolike

    Joined:
    Jan 17, 2020
    Posts:
    31
    I am trying to make a physics 2D game. Various shapes fall down into an area that bounded by screen size. I added the ground and added walls on the top and sides as well.

    When I run it in the Unity Editor, everything works perfectly. However, when I run it on a device like iPhone, some weird things happen. The shapes will start shaking, sometimes overlapped. I've tried every solution I can find, but none of them worked.

    It has bugged me for 3 weeks. I've attached a video showing this issue.

    Has anyone experienced this before? Please help me.

     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Not enough detail to help you beyond guess after guess.

    In the very least, please explain/show what each of these circles are comprised of. When is the simulation running? FixedUpdate? Update? What global physics settings have you changed? Are you using the default iterations or have you tuned them?

    Guess#1: If each has a Rigidbody2D, is it using Discrete or Continuous collision detection? Discrete will causes overlaps, especially evident in such confined set-ups.
     
  3. uncolike

    uncolike

    Joined:
    Jan 17, 2020
    Posts:
    31
    Thank you for your reply.

    When the game starts, the shape is static on the top the the screen. When screen is touched, the shape is changed to dynamic by the code below.

    Code (CSharp):
    1.  
    2. float fallVelocity = 10f;
    3. GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
    4. GetComponent<Rigidbody2D>().velocity = Vector2.down * fallVelocity;
    5.  
    These shapes includes Collider 2D and Rigidbody 2D. The following screenshot.


     

    Attached Files:

  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    All looks okay in the screenshots albeit you're not using interpolation which will give you smoother visuals but that doesn't affect the simulation itself.

    You are using the default iterations for the solver. I presume you're using the default fixed-update rate of 50Hz and not changing this on the device for instance? Also, I presume you're not using large mass differences for these bodies; I see mass as 0.1. Are they all that? The only thing to be careful with mass is that you try to not go beyond a 10:1 mass ratio in situations like this with things tighly packed and interacting.

    I presume there are no other scripts on each of these "balls" that cause movement of any kind? Arer you using multithreading perhaps? If so, turn that off and see if it affects it.

    I still cannot tell you what is going wrong though. Certainly start with the basics such as attaching a profiler and seeing if you're getting poor update rates.
     
  5. uncolike

    uncolike

    Joined:
    Jan 17, 2020
    Posts:
    31
    Thank you for the debugging directions.

    You reminded me that there is actually a script to change the scale of the ball. Now I added a child body with a sprite renderer. The scaling animation script will use this child instead of scaling the parent with rigidbody2d and collider2d. The mass of the ball is between 0.1 and 0.3.
    I have checked the Fixed Timestep(0.02) and multithreading (off) .

    If you don't mind, I'd like to add a video showing you another strange phenomenon.
    At the bottom of the screen there is a ground object which only has collider2d and SpriteRenderer. during the game, for some reason, the collider path on the ground seems to move down overall.
    I tested it on iPhone 13 Pro, iPhone 6 and Google pixel 2 XL. It seems that this problem ONLY occurs on iOS.

    BTW, I adjusted the timescale only for showing you the details.

     
  6. uncolike

    uncolike

    Joined:
    Jan 17, 2020
    Posts:
    31
    This problem was solved after I downgraded Unity Editor from 2022.2.x to 2021.3.