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

Colliders and sprites with many poses

Discussion in '2D' started by Scottes, Jun 24, 2020.

  1. Scottes

    Scottes

    Joined:
    May 22, 2020
    Posts:
    5
    I'm working on a 2d Platformer using a bunch of sprites with many poses, and I'm having issues understanding how best to deal with colliders.

    colliders.png

    In the first image I'm using a capsule collider 2d. Movement is fine and this works in every situation - except the one show. When the player goes to shoot his arm and gun can be embedded in a solid object.

    In the second image I added a box collider 2d, and this fixes the issue with the arm and gun getting embedded in solid objects.

    The box collider has an issue, seen in the third image, where the player gets stuck when falling because the edge of the box collider gets caught on the edge of a solid. What's the best way to deal with this?

    It also has a more minor issue where the player can't get "realistically" close to a solid when the gun is not extended.

    I went through all the animations frame by frame and adjusted both colliders to fit each frame, resizing and/or moving the colliders to match the extents of each sprite used. This causes other issues. If I move against a wall and keep pressing the move key the player slides back and forth as the width of the box collider changes. Is it better to use a box collider of a constant width?

    I did try a Polygon 2D collider, but this seems overly complex - especially when I tried to adjust the polygon for each frame. Is there a way to get a simple polygon collider - perhaps with 8 vertices? Or should I add a polygon, reduce it to 8 vertices, and then adjust for each frame>

    Any suggestions on dealing with this would be greatly appreciated.
     
  2. Neomedus

    Neomedus

    Joined:
    May 14, 2020
    Posts:
    47
    It looks like your problem could be solved by having it so when you jump, the box collider is disabled. You could do this by attaching the box collider to a parent gameObject and then using this https://docs.unity3d.com/ScriptReference/Collider-enabled.html to disable it when you jump, and the reenabling it once you touch the ground.
     
  3. Scottes

    Scottes

    Joined:
    May 22, 2020
    Posts:
    5
    I had actually thought of that, but disabling it when in the air would then allow the player to enter a wall, rather than falling alongside it. If that makes sense.
     
  4. Neomedus

    Neomedus

    Joined:
    May 14, 2020
    Posts:
    47
    Maybe instead of a box collider you could just place a bigger capsule collider over the object?
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    It's not clear why you want the box to be against the floor either. Wouldn't the box be best suited overlapping the arm. Also, something to experiment with is the EdgeRadius property you can use on the BoxCollider2D that rounds the vertex. A circle at the end of the gun would perhaps suffice but it all depends on your movement code.
     
    Scottes likes this.
  6. Scottes

    Scottes

    Joined:
    May 22, 2020
    Posts:
    5
    @Neodemus - I'd actually like to keep the capsule collider as a hit box used to detect enemy projectile hits.

    @MelvMay - I don't think this is an issue. A short, but wide box collider placed at his midriff could still easily catch on platforms like the third image in the image above. There is another sprite where the player is falling but shooting, with the arm and gun extended. If a box collider encompassed the arm and gun then the collider would hang up on a platform.

    The Edge Radius might do what I need though. I did not know about that. I will look into this. Thanks.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    But I would've thought that iuf you're not in a walking state you wouldn't use this collider. I'm not suggesting you have some wide collider when falling and not have it collide with something. It seems you only want this thing active when walking.
     
  8. Scottes

    Scottes

    Joined:
    May 22, 2020
    Posts:
    5
    Not true. It would be needed when falling to ensure that the sprite can't enter a wall.
    Imagine the player falling while near a wall. If the player had lateral movement, the lack of a non-trigger collider would allow the sprite to fall into the wall. Like this:
    colliders2.png
     
  9. Scottes

    Scottes

    Joined:
    May 22, 2020
    Posts:
    5
    Hmmmm. I tried a Polygon Collider 2D, reduced to 8 points, but it does not seem to be able to change the collider shape on every animation frame like I can with the box collider 2D. That is, I changed the polygon shape for my Idle sequence, then went to the jumping sequence and changed it again. Even though I had used the record button, the shape remained as the last modified shape no matter which sequence was playing.

    Edit: Seems like one can't record changes for the polygon points, only the offset, which is the only red item when the record button is active. Bummer.
     
    Last edited: Jun 24, 2020