Search Unity

Unexpectable physics behavior on circle colliding with box

Discussion in '2D' started by artyompi, Aug 20, 2017.

  1. artyompi

    artyompi

    Joined:
    Aug 20, 2017
    Posts:
    2
    Hi,

    I'm trying to do simple 2D runner, wich generate road in runtime.
    So I wrote a script. It instantinates simple 1x1 boxes in sequence. Y-coordinate is same for all blocks. Every next box has X-coordinate with a value Xprev+1. Every box has box collider with same size as a box.
    Player is a simple circle with circle collider and rigid body. Z-rotation is fixed. Player script in update sets velocity, so velocity would be constant. Colliders has no materials.



    Every thing is OK, but the circle rebounds from the floor (sequence of boxes with same Y-cordinate). It looks like circle bumps into the edge of box, but it can't, becasue Y-cordinate is same for all boxes, and where no gaps between boxes.

    So, can you help me to recognize, why it is happening and fix it?

    P.S. Sorry for my English - I'm not native speaker.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,473
    This is a common problem with physics systems. The "floor" isn't a continuous surface, it's composed of separate colliders. Collisions are calculated between colliders, not groups of colliders. It's possible therefore for the circle to catch the corners of the boxes as I presume you've got gravity pulling it down into them.

    The EdgeCollider2D can produce a single continuous surfaces made of multiple edges and solves this problem. If you must have multiple colliders then you can merge them together using the CompositeCollider2D and set it to use "Outline" generation (continuous edges).
     
    artyompi and LiterallyJeff like this.
  3. artyompi

    artyompi

    Joined:
    Aug 20, 2017
    Posts:
    2
    Thank you! CompositeCollider2D is what I need.
     
    MelvMay likes this.