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 /
  • Help Room /
avatar image
4
Question by tribski · Sep 25, 2016 at 10:27 PM · scripting problemcollider2dpolygon collider 2d

Can you get polygon collider 2d shape from a sprite?

When you create an object with polygon collider 2d from inspector, unity automatically generates polygon collider that approximately fits the sprite. My question is if there is a way to do that when creating an object from a script? While searching for a solution I found that each sprite has a sprite mesh which has vertices. I tried setting those as a path for my collider, but the result is a mess: their order appears to be messed up. My current code:

         Sprite spr; // I get that in other part of the code
         GameObject go = (GameObject)Instantiate(prefab, gameObject.transform);
         go.GetComponent<SpriteRenderer>().sprite = spr;
         go.GetComponent<PolygonCollider2D>().SetPath(0, spr.vertices);
       
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

2 Replies

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

Answer by tribski · Sep 26, 2016 at 02:15 AM

There appears to be a somewhat good solution: add new polygon collider ( and remove the old one). When you add new collider with go.AddComponent<PolygonCollider2D>(); it will be generated to fit the mesh.

Comment
Add comment · Show 2 · 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 LeroyKim92 · Feb 02, 2017 at 05:08 AM 0
Share

I don't know if it's best practice or not but it works great! Thanks!

avatar image KiryaRizhiy · May 07, 2020 at 03:40 PM 0
Share

This way is working in editor but not in android device. Have any idea how to fix such problem?

avatar image
8

Answer by CaseyHofland · Sep 12, 2020 at 09:09 AM

For anyone finding this, there is a better way than OP's answer (sorry senpai).

 // Store these outside the method so it can reuse the Lists (free performance)
 private List<Vector2> points = new List<Vector2>();
 private List<Vector2> simplifiedPoints = new List<Vector2>();

 public void UpdatePolygonCollider2D(float tolerance = 0.05f)
 {
     polygonCollider2D.pathCount = sprite.GetPhysicsShapeCount();
     for(int i = 0; i < polygonCollider2D.pathCount; i++)
     {
         sprite.GetPhysicsShape(i, points);
         LineUtility.Simplify(points, tolerance, simplifiedPoints);
         polygonCollider2D.SetPath(i, simplifiedPoints);
     }
 }


This way you don't have to destroy / add a new PolygonCollider2D each time, plus the points List can be recycled for some free optimization.

Thanks to @empath for pointing me towards LineUtility.Simplify!

Comment
Add comment · Show 1 · 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 empath · Sep 25, 2020 at 04:07 AM 1
Share

@CaseyHofland

Your method is pretty amazing, it's cool seeing such a tight collider around a character model.

That said... there are some extreme performance ramifications for doing so. Even when 2 objects with only polygon colliders on them that are set to not detect one another in the project settings touch are laid on top of one another, my FPS drops by 20. (This seems unintended?)

You mention doing point optimization using DouglasPeukerReduction, is that something easily copyable?

EDIT: I feel like a fool. I swear I already searched and couldn't find anything. I don't know if this is what you used, but for anyone else curious, this seems to do the trick: https://docs.unity3d.com/ScriptReference/LineUtility.Simplify.html

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

91 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 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

Help with making an object dodge other objects? 0 Answers

How can i get a transform to move to another transform if it is touching it? 0 Answers

Two colliders that trigger different things in the same object-hierarchy 0 Answers

Collider2D.Cast doesn't detect some parts of my composite collider 2D 0 Answers

4 line code not working! 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