Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SeigneurNecron · Mar 12, 2018 at 06:29 PM · collider2dmaskgenerated

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.

cone-shaped attack image

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?

generatedattackarea.png (10.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by SeigneurNecron · Jun 21, 2018 at 02:29 PM

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. alt text 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 alt text
- 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:
https://assetstore.unity.com/packages/tools/sprite-management/shapes2d-make-art-fast-62586

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.


generatedattackarea2.png (9.7 kB)
generatedattackarea1.png (12.7 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

75 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2d sprite mask shader with polygon collider? 1 Answer

Need advice on making a progressive " loading GUITexture" 1 Answer

how to create illumination mask texture loading script? (self-illuminated diffuse) 1 Answer

is it possible to make 2d real time coloring with masks (paint app) 1 Answer

Can Layer Masks Achieve This Effect 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges