0
\$\begingroup\$

How to align the Polygon Collider 2D in Unity to match the position of Sprite?

I have a game object. It has a Sprite Renderer with an image. Now I add a Polygon Collider 2D component to the game object, that generates a collider. The shape of the generated collider is correct as expected - i.e. it matches the image from the Sprite Renderer. But the position of the collider is wrong. Here you can see what I mean:

enter image description here

For sure I have a solution - I can manually configure the Offset of the Polygon Collider 2D. But that would take a significant amount of efforts and time (because I have a lot of such game objects, which need to have a sprite and collider). So, I am hoping to find a solution which would position the generated collider correctly. I.e. in such a way, so that the boundaries of the collider would match the boundaries of the sprite.

enter image description here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You probably don't want to use a polygon collider for this purpose. Most platformers / run and gun games will use a simple primitive for each character's collision - like a capsule, box, or circle. That keeps the setup simpler, faster to process, and makes it feel more consistent for the player. They don't need to attend to every pixel of the character's skinned animation on every frame - their controls / hit box always behave exactly the same way. \$\endgroup\$ – DMGregory Jan 20 at 15:06
  • \$\begingroup\$ @DMGregory, I understand your point. But I want the collider to be precise here. That is my intention. \$\endgroup\$ – some1 here Jan 20 at 15:40
1
\$\begingroup\$

The issue most likely has to do with the skin & bone animation. The polygon collider will not update itself every frame while the sprite is animating.

Your best option would be to use a simple collider instead, but if you really need it to be very accurate, you'll need the colliders to be placed on the objects and updated as they rotate/move around.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.