Calculate BoxCollider2D based on the actual player sprite
Hey guys, first of all my initial situation: I am working on a 2D project. The player consists of a spritesheet and has small animations. The animations are not always exactly as big as the idle form of the player. It scales a little bit in width and height.
Now I am wondering if it is possible to calculate the BoxCollider2D based on the actual size of each sprite.
Can anyone here give me some guidance on how to solve this situation? Best regards :)
Answer by tylerdtrudeau · Mar 04, 2021 at 08:10 PM
For this I would use a PolygonCollider2D instead of BoxCollider2D. Generally when you have a sprite of any random shape (lets say a player standing) if you add component of type PolygonCollider2D, it will automatically trace around the edges of your sprite and made a collider of that shape (meaning it will make a collider around the legs, arms, head, body, etc....) then you can edit the collider (the edit button should be the first field in the polygon collider component) Editing the collider will allow you to adjust exactly what shape you want it to be. Also sometimes the polygon doesnt draw it perfectly onto your sprite so you just have to go into edit and adjust it slightly.
Let me know if this works!
Answer by xKarToSx · Mar 09, 2021 at 07:37 PM
Thanks for your Answer @tylerdtrudeau, i used your suggestion with PolygonCollider2D. It worked directly. 2 animations I had to correct only. Thanks for the help again. ;)