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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D boxcollider stuck with other angle

Discussion in '2D' started by Pob_Fr, Jun 12, 2015.

  1. Pob_Fr

    Pob_Fr

    Joined:
    Jun 12, 2015
    Posts:
    6
    Hi,

    I begin with Unity and try to make a quick plateformer 2D.

    I move my "player" on multiple tiles and he stuck sometimes in the corner of the next tile. I have see some post with the same problem but never a solution without change the box-collider of my player in circle-collider, and I want keep a box.

    I use velocity for the movement.

    Here what it's look like. (I move to the right)


    Thanks for your help !

    Oh. And sorry if my english is terrible :p
     

    Attached Files:

  2. Kuan

    Kuan

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    87
    @Pob_Fr. I afraid the involvement of circle collider is the most common and correct fix to your problem if you want to use physics to move your player GameObject. You might need some very tricky workaround if you insist on using a box collider on your player GameObject. Is there any reason you must use box collider for your player?
     
  3. Pob_Fr

    Pob_Fr

    Joined:
    Jun 12, 2015
    Posts:
    6
    Thanks for the answer Kuan.

    And yes, I want use a box because the player is the a box :p
    I have already try with circle collider but there still little bug. Like the object is "bumping" on angle when is moving.

    There is no "simpler physic" in Unity ?
     
  4. Kuan

    Kuan

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    87
    Oh well, it's obviously a box shape player, what am I asking :rolleyes:.

    It really boils down to how you want your games to behave, then implement around it. If you games is a very "boxy" games like shown in your attached picture, then instead of forming your ground with a list of box game object with box colliders, you can consider to form the ground with box game objects (without box collider on each of them) and then create one edge collider / box collider that cover up the group of ground's gameobject. In this case, you will have a flat surface instead of a bumpy one.

    And... what do you mean by simpler physic?
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    The real problem is not so much the player BoxCollider2D but the construction of a continuous surface using multiple BoxCollider2D. No matter how close you put them, they're still separate boxes with corners that (due to precision) will cause you to collide with them.

    The real solution is to use a single EdgeCollider2D to create a continuous surface and it's also better for performance. Even using a single BoxCollider2D to cover the surface is better.

    If you absolutely must have a collider for each tile on that surface then consider using the EdgeCollider2D with a single edge and increase the Y offset so it sits on 'top' of the tile-sprite.

    Whilst it's convenient to associate both a sprite and a collider on a GameObject then copy it everywhere, it's not the best for the physics system unfortunately.

    The smart-sprites will help with this situation in the future.
     
    theANMATOR2b likes this.
  6. Pob_Fr

    Pob_Fr

    Joined:
    Jun 12, 2015
    Posts:
    6
    Thanks you 2 for your answer.

    @Kuan When I say simpler physic i think like the first Mario. You jump. You fall. And stop when hit something. No hill, no weight, etc...

    @MelvMay Yeah I will use EdgeCollider2D. But I don't like them because I can't place edge in exact position (or I don't see the option)

    Thanks for your help !
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    Note sure what you mean by exact position but all 2D colliders have an offset property (http://docs.unity3d.com/ScriptReference/Collider2D-offset.html) for positioning local to the GameObject its on. The real problem is that if you treat each tile as a GameObject with a collider then you just end-up with lots of single edges rather than a continuous set of edges on one component.
     
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,386
    You could put circle colliders on the corners that are slightly outside the interior box collider bounds. That would smooth out inter-tile collision jutter like that.