Unity Collision Hitbox for 2D Sprites

Hello! I’m making a game for a school project, and I had found a tutorial online for collision, but I feel as the collision is too… wide.

Attached an example of what I mean! The Orange Outline is the sprite itself, whilst the Green outline is the collision! Is there anyway for me to make the Green Outline the same as the Orange Outline, or shrink it down?


I’m using Tilemap Collider 2D, Rigidbody 2D, Composite Collider 2D.

Add a polygon collider 2D and edit it by pressing the edit button

1 Like

If these are tiles in a tilemap, then you may want to check the sprite editor to see what physics shape has been set for each tile.
For example, if I just let Unity figure out the physics shape itself, it will often create a shape that is too big. Take a look at the slope sprite in the picture below.

But you can edit the shape to match how you want the collision to work:
9802356--1407186--upload_2024-4-28_20-32-40.png

So, I’ve did what you suggested, and edited my sprites in the sprite editor. For example, I’ve edited one of my lamp posts, in the custom physics shape.


However, whenever I go into play mode, and test out the physics, it’s… confusing.

I can seemingly phase through the bottom part of my lamp post, but there’s just this chunk on top that just… stops me from walking through it. I’ve honestly go to idea what’s wrong with this.
9804714--1407708--test 2.PNG 9805647--1407963--upload_2024-4-30_9-19-46.png

dont put these tall objects that you are supposed to walk behind on tile maps

make normal game objects for the lamp post

Assuming the lamp is three tiles, you’d only put a physics shape area around the very base of the lamp where it meets the floor (projected against the floor). You have to imagine that the “box” you’re putting on is projected against the floor.

For clarification, in this second image you posted, is this the closest that you can get to the top of the lamp? Is this where the collision stops your character?

9805647--1407963--upload_2024-4-30_9-19-46.png

that’s correct! however, after taking a look over all of your suggestions (to where i am very thankful), ive figured out most of it! although, how do i make my character go behind the lamp? is it via a different ordering of layers?

Ive currently got
1- background
2- foreground
3- player for my layers

okay, on second thought now, ive done all the suggestions in this thread, yet it still persists.

1 - I’ve made the lamp into it’s own game object, instead of being a background tilemap.
9811803--1409277--upload_2024-5-3_18-24-50.png

2 - I’m only using Polygon Collider 2D for this, as seen by the green outline,

I can walk through the top of the lamp just fine, but whenever I get to the bottom, there is just huge boarder that harbors the bottom of the lamp.

9811803--1409280--upload_2024-5-3_18-26-55.png
thats the closest i can get to the tile. I’m starting to think its probably my character’s sprite hitbox, or something in my coding thats blocking this.

okay, after playing around with my code and script, ive discovered what went wrong

my character was constantly taking a tile for each step, and when i reduced the walk to 0.25 per step, that pretty much instantly fixed it.

however, the issue now remains that the hitbox is rather wonky, and the bottom part of the pole is still sticking out.

good the lamp should be its own game object

now instead of making it a composite colider

make it a box collider

then make it so that the box only covers the bottom of the lamp

then put the pivot of the lamp sprite on the bottom of the pole

put also the pivot of your character on the feet of your character

then use transparency sort axis, make it sort by y, (0,1,0)

then make sure your char is on the same layer as the lamp, and both have same sort in layer order

you should now be able to walk behind the lamp

2 Likes

Thank you so much! It works, now!

1 Like

Okay, small issue. I can get it to work, but if I save + quit, when I come back, my character goes over the lamp now, and I have to fiddle with iti a bit for him to go back. It happens every time I save, got any ideas?

do you mean when you save and quit unity??

anyway, you should check if you set the transparency sort axis correctly, and the pivots on the sprites