Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Morvs · Jun 29, 2016 at 07:22 AM · 2d gamespriterenderermouseclickpolygon collider 2dintersection

Is PolygonCollider2D a legit way to check for mouse clicks in irregular shaped sprites?

Hi there,

I'm new to Unity and always wanted to create a strategy game inspired by Europa Universalis or Hearts of Iron.

So my first goal was to be able to click on a province to select them. For now I illustrate that by changing the color of the province. I'm using a PolygonCollider2D for each province and adjust the collider to the shape of the sprite.

It then looks like this: alt text

And like this for all the 3 provinces(for now): alt text

So selection is working fine, the province graphics are just white so that I can simply set the color when the mouse click happens to be on the province. It's also useful that by using the colliders I can simply zoom in and out and the selection still works fine. Don't bother about the province intersections, there are going to be border graphics overlapping them once I'm coming forward.

My concerns are:

  1. Performance: There are going be several more, over hundred, additional provinces, each of them having one of these colliders. If the mouse is clicked somewhere is every PolygonCollider2D checking in detail for itself or are they first checking for rectangle bounds? There will also be other graphics placed upon the provinces which may need a PolygonCollider2D.

  2. Is this the, so to say, legit way of accomplishing my goal in Unity apart from performance issues? I read something about raycasting and intersecting a ray with a plane(the sprites) but as soon as I put something else on the province I'd also need to check for the color of the pixel as Unity is returning a bunch of intersected pixels(if I unterstood it correctly). With the current approach I just need to adjust the Z coordinate of the graphic on the province by a minimal value so that it's really placed over the province and it is selected instead of the province.

I hope I've been clear enough and if I haven't I'll try to fix it :-)

Thanks for every help!

bildschirmfoto-2016-06-26-um-013256.png (74.3 kB)
bildschirmfoto-2016-06-26-um-012744.png (73.8 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
1
Best Answer

Answer by CodeElemental · Jun 29, 2016 at 07:45 AM

  1. Regarding the first issue you are safe from performance impact caused by the colliders. Colliders are processed by the physics engine and since you only use them in an "inert" state (they do not actually collide, they do not move etc..) the physics engine will not be burdened. Also, a single raycast for touch/mouse detection does not qualify as 'heavy lifting' for unity.

  2. Regarding the second issue, I would propose "province detection" of some different kind than pixel-type. Maybe create some kind of Script that will hold the relevant data [name, player who controlls it etc etc..] and check the values upon raycasthit. Furthermore, you can separate the province terrain from province objects by placing them in separate layers. You can get the distance to all hit objects and you can process the logic only for the closest one.

Hope this helps you in your journey :)

Comment
Add comment · Show 3 · 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
avatar image Morvs · Jun 29, 2016 at 08:49 AM 0
Share

So I'm save to do it with the colliders.

Also, if I'm getting this right, the colliders and the raycasthit approach don't really differ that much in my case but the collider already does some stuff for me that I therefore don't need to fiddle with. About the layers, it sounds like they're practically behaving the same as if I'm increasing the Z value by this $$anonymous$$imal value what I described. As the value is so small it also doesn't affect the visualization, so no problems in sight for now.

About the script, that's of course going to be written. All the data will be seperate from the visual stuff but I need a simple way to know which province is selected. That's possible now. I'll just connect the SpriteRenderer and the data class in some good way. I'll see about that.

Thank you very much, definitely helped me to get me going :-)

avatar image CodeElemental Morvs · Jun 29, 2016 at 09:00 AM 0
Share

$$anonymous$$y idea was to maybe create a script like

 public class Province : $$anonymous$$onoBehaviour
 {
   public string Name; // The name.
   public int Type; // maybe relevant for visual representation
   public string PlayerOwner; // relevant for visual representation
 }

Which you attach to the province collider. Then you can check the RaycastHit2D for

 if (hit.collider.gameObject.layer == Layer$$anonymous$$ask.NameToLayer("Provinces"))
 {
   // To get the name : hit.collider.gameObject.GetComponent<Province>().Name;
 }


avatar image Morvs CodeElemental · Jun 29, 2016 at 09:07 AM 0
Share

Aye, I will look into this RayCastHit2D stuff later in the evening so that I understand it. Thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Polygon Collider 2d doesnt work properly 3 Answers

calling setTile(location, null) freezes game for half a second 0 Answers

Puppet animations by sorting layers, generate overposition in prefab 1 Answer

Can I use the Sprite Mode Polygon Editor to outline multiple sprites in one image atlas? Ifso, howso? 0 Answers

How do you make a circular player? 2 Answers


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