Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by johkar · Apr 07, 2014 at 01:51 AM · spritecollider2dpolygoncollider2d

Editing Polygon Collider 2D Mesh by Script

Hey guys, I am working on an isometric 2D terrain generation with which I want to be able to interact with the mouse later. I am creating all the GameObjects within a script and need the tiles to have a Polygon Collider 2D for the mouse interaction. I am far from satisfied with the mesh Unity creates for each tile and want to edit them manually. I do know that I could create a Prefab and edit the Polygon Collider there but I want the colliders to be as precise as possible so that there are no holes and no overlaps in the final terrain and the best way to do that is by placing them in a script with precise coordinates.

![image][1]

I have attached an image with three current phases I am encountering. The first kind(left) happens when I create the GameObject with a script and add the Polygon Collider 2D as a component. The second one(top right) is when I throw the sprite into the scene myself and add the Polygon Collider 2D in the Inspector. And the final one(bottom right) is the way I want it to be only the gras should be seen as a collider.

I have started to access the Polygon Collider 2D component in a script but can't think of a way check for current placement of the vertices of the collider mesh or how to edit them. [1]: /storage/temp/24857-polycollider2d.jpg

polycollider2d.jpg (451.2 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

Answer by Leuthil · Dec 23, 2015 at 05:40 PM

I have found that just using SetPath of a PolygonCollider2D can sometimes behave strangely, especially if you are doing multiple paths. I found that setting pathCount first prevents this chance of strange behaviour.

 polygonCollider2d.pathCount = 1;
 // firstPath is an array of Vector2's defining all of the points in your collider
 polygonCollider2d.SetPath(0, firstPath);

Or for more than one path:

 polygonCollider2d.pathCount = 3;
 polygonCollider2d.SetPath(0, firstPath);
 polygonCollider2d.SetPath(1, secondPath);
 polygonCollider2d.SetPath(2, thirdPath);


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 Waz · Dec 19, 2016 at 01:34 AM 2
Share

A small warning: at least in Unity 5.5, SetPath seems extraordinarily inefficient, spending nearly all it's time in "Physics2d.ColliderCleanup" as the number of points increases.

You can avoid this problem by disabling the PolygonCollider2D while setting the paths:

  polygonCollider2d.enabled = false;
  polygonCollider2d.pathCount = 3;
  polygonCollider2d.SetPath(0, firstPath);
  polygonCollider2d.SetPath(1, secondPath);
  polygonCollider2d.SetPath(2, thirdPath);
  polygonCollider2d.enabled = true;

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

23 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

Related Questions

Polygon Collider 2D Not Precise Enough 0 Answers

2D Sprite, Animation Question 1 Answer

Why doesn't my hexagonal collision fit properly? 1 Answer

How to update EdgeCollider2D with sprite animation? 1 Answer

Tile Generation with collider2D's 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