How to get some kind of SpriteMask from a PolygonCollider2D ?

Hi everyone, I’m making a 2D game and I’m trying to implement a cone-shaped attack that is blocked by walls. Let’s start with an image so you can better see what I’m talking about.

I want to get a collider (PolygonCollider2D) and some kind of sprite mask to display particles inside the orange “This is gonna hurt” area.

The walls and the bad guy will be static, so I only have to generate the collider once in an editor script (this is pretty much done). The real problem is getting the sprite mask for my particles, I didn’t find anything like a “collider mask”. Any advice on achieving that?

I’m gonna answer my own question, just in case someone is in the same situation.

Part 1 - Generating a PolygonCollider2D:

I wrote a script that solve this problem in this particular case:

  • the level is only built with BoxCollider2D (that can be scaled and rotated)

  • the level is static, the walls and the enemy won’t move, so the polygon only have to be generated once in edit mode.

The script is quite long and uses other tools I made, so I won’t paste it here, but I will describe the steps, with text and images.

First, I do a lot of ray casting from the enemy, a lot more than on the image (2 rays per degree), so I don’t miss parts of the terrain.
For each raycast, I register three things : the angle of the raycast, the position that was hit, and the associated BoxCollider2D. I store this data in custom data structures I made, cause I need to store additional data that I compute from this, and order is important.

Now that we have all this data we can start to make deductions. The following image contains lots of stuff, read the text to understand what’s going on. xD

  • The first 3 points - in purple on the image - are easy.

  • I used two shades of blue to highlight each BoxCollider2D that was hit by at least one raycast.

  • From these colliders, and from the position of the enemy relative to these colliders, we know which corners will potentially be part of the polygon. I placed red/yellow circles on these corners.

  • From the order of the points we got by casting rays all around the place, the corners of the colliders, and the computed theoretical angle we would need to raycast to hit these corners, we know if a corner will be part of the polygon (red) or not (yellow) and if we need to do an additional raycast from that corner to get a new (orange) point on a collider that is behind (disable the collider you are casting from during the raycast).

  • And that’s it! We have all points of our polygon. All we need to do now is to register this in the points field of our PolygonCollider2D.

Part 2 - Generating a Sprite to use in a SpriteMask:

For this, I used the free asset Shapes2D from the asset store. This tool can generate a Sprite from the PolygonCollider2D we just made. Here is a link to the asset store page:

Again, this is ok for me because I only need to generate the collider and image once, in edit mode. If you need to dynamically generate this during play mode, this may not be the best solution, but it may still work.