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.

Question Sliding Physics

Discussion in 'Physics' started by VeganbonD, May 16, 2020.

  1. VeganbonD

    VeganbonD

    Joined:
    May 7, 2020
    Posts:
    3
    Hi,

    I making a puzzle game where you have diamonds that slide on each other and the player click to eliminate them.
    I am using physics to get the effect. However sometimes they just hand even though they have a free space.
    stuff like microscopic height difference stop the sliding. is there a way to overcome it?

    (In motion)


    and this sometimes happen, which is more critical.

    Thanks!
    upload_2020-5-16_22-38-13.png
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    Personally, I wouldn’t use physics for that unless there’s more to the game that really benefits from physics. Physics will always have problems as the simulation will never be 100% predictable and reliable
     
  3. VeganbonD

    VeganbonD

    Joined:
    May 7, 2020
    Posts:
    3
    Thanks yeah, that was my second approch. I had a fantasy where it will be all done by the physics with tweaks lol.
     
  4. Carlosgrr

    Carlosgrr

    Joined:
    Jun 20, 2019
    Posts:
    8
  5. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    The problem will be that the corners of the boxes are getting snagged on each other. If you want to continue prototyping then you might be able to get away with boxes with cut off corners so there's not corners for the boxes to snag on. However you'd still need some way to make only one box slide into place if a box below two boxes is removed, otherwise they'll both slide and then jam up part way into the open slot....

    Just don't use physics.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,811
    It's hard to tell from your game but there are a few things you might try before you give up on using physics.

    If this is 2D physics and those are BoxCollider2D (rotated) then you can add a small EdgeRadius to reduce/eliminate this effect of catching corners of other boxes. If the ground is all composed of these then the likelyhood of this causing you a problem with an edge radius is very small.

    Alternately or additionally if for the ground you have a parent CompositeCollider2D set to "Outline" mode, when the falling piece stops in its final position (as a child GameObject of this) you can set its BoxCollider2D.usedByComposite to true so it gets merged into the ground. Outline mode produces continuous Edge outlines that don't suffer from this problem. Be aware though that if there are too many pieces in the ground then, depending on the device this runs on, you might see CPU spikes calculating those outlines. Something you could profile though.

    All the above obviously won't affect visuals which will continue to look identical i.e. sharp corners etc.