Search Unity

Sprite snagging

Discussion in '2D' started by Avo, Dec 1, 2013.

  1. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    I've currently set up a little 2d scene where I have a character who walks around on a bunch of boxcollider 2d's. They're all perfectly aligned and yet sometimes my character snag's on them. With a 3d game I'd increase the skin variable on the character controller to fix this but I'm unsure of how to fix this on a 2d character. Thanks for your time!
     
    Last edited: Dec 1, 2013
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    What kind of collider are you using on the character?
     
  3. Gesh

    Gesh

    Joined:
    Feb 23, 2013
    Posts:
    6
    I also have this problem, I have 500 tiles aligned all 50x50 with a 32x32 player, when moving my character it randomly gets stuck on tiles like there's an invisible wall.. it's also completely random but happens fairly often.. when it happens I must slightly change direction and then I can continue going the direction I was going.

    All of the tiles are using a box collider and so is the player.
    *I enabled Fixed Angle on the character's rigidbody, with it disabled the character just flips all over the place as soon as it hits a snag.
     
  4. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    The best way to get around the snag (I think it's a Box2d issue) is to use a circle collider as the base of your player and a box for the top. I setup a quick scene myself and can verify this will eliminate your problem.

    $character-collider.png

    Edit: You may want to avoid intersecting colliders if you're using collision detection scripts so just make sure the circle doesn't go through the box collider and you're all set.
     
  5. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    Sorry for my late response Unity. I was indeed using only a box collider. I saw that they used a circle and a box collider in their demo but I thought that had to do with the shape of their character instead of moving around. Thank you for your help Unitylover.
     
  6. murphyvjamese

    murphyvjamese

    Joined:
    Apr 19, 2023
    Posts:
    4
    The trouble with the circle collider is that when it snags, instead of getting stuck, it bounces up a little, and if you try to jump during that time, you won't be able to. So though it is a little bit better than the box collider, it is still not an ideal fix.

    I have been able to consistently reproduce the bug with a cpu character that starts and stops moving on a constant time interval, and whose velocity changes immediately and directly (as opposed to accelerating via rigidbody force application. If it stops between two tiles, and is moving slower, this issue occurs consistently, but I still do not know why it chooses to snag at that particular location, and not the others.)

    Using this as my testing environment, I tried to unite all my ground tiles under a single parent object with a composite collider 2d, but that also failed to fix the issue. (I suppose it is possible I am configuring that design incorrectly, though.)

    For now, I am catching the error in my scripts and then responding to it by moving the player over by 1px with the transform component. Still, this seems like a common enough issue that I'm suprised Unity doesn't have a built-in solution for it. (Or if there is one, I'd love to know!)

    Note: I am not using the tilemap system
     
    Last edited: Dec 15, 2023
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    Please create your own posts, don't hijack/necro 10 year old threads for your problem.

    Well not modifying the Transform is a start, you should never do this. Also, physics is not and never was pixel-based.

    I'm not going into detail as to why ALL physics engines suffer from this problem (search for "ghost collisions" or "ghost vertices") as it's been described to death here on the forums and elsewhere but 2D physics (Box2D) has primitive edge chains which do not have this issue and allows chains of edges that join naturally without issue. I exposed this with the EdgeCollider2D and CompositeCollider2D (in Outline mode) many years ago. Adding a CompositeCollider2D isn't a fix for a specific problem, it's a component that provides various features so you need to understand that part. If you only wish to merge together colliders into a single collider and produce continuous edges so you don't get "ghost collisions" then that's what it can do for you.

    Thanks.