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
0
Question by Distant-Object · Feb 03, 2014 at 09:23 PM · vertexcollider 2dpolygon collider 2d

How to get the closest vertex of the Polygon Collider 2D in code?

Hi,

Title pretty much explains the entire question. 3D colliders have a function called ClosestPointOnBounds, but that is for the bounding box and not the collider mesh itself(?).

Alternatively, how do I access the vertices that make up a collider in code? col.poly.paths nor col.ColliderInfo.Vertices works.

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 robertbu · Feb 03, 2014 at 10:00 PM

You use the points array of the PolygonCollider2D. These points are in local space, so you will have to convert them to world space before doing the comparison (or you could convert the world to local and do the comparison in local space). Here is a bit of code to demonstrate. Attach it do a game object with a PolygonCollider2D. Create a marker object (I created a sphere of size (.1,.1,.1), and drag and drop it on the 'Display' variable in the Inspector. Run the app and click in the Game window. The display object will be moved to the closes vertex on the collider.

Note I wrote it for both an Orthographic and Perspective camera. The code could be simplified a bit if it was only going to be used on an Orthographic camera.

 #pragma strict
 
 public var display : Transform;
 private var poly : PolygonCollider2D;
 
 function Start () {
     poly = GetComponent(PolygonCollider2D);
 }
 
 function Update () {
     if (Input.GetMouseButtonDown(0)) {
         var pos = Input.mousePosition;
         pos.z = transform.position.z - Camera.main.transform.position.z;
         pos = Camera.main.ScreenToWorldPoint(pos);
         
         var placement : Vector3 = FindClosest(poly, pos);
         placement.z = transform.position.z;
         display.position = placement;
     }
 }
 
 function FindClosest(poly : PolygonCollider2D, point : Vector2) : Vector2 {
     var dist = Mathf.Infinity;
     var result = Vector3.zero;
     
     for (var corner : Vector2 in poly.points) {
         var test = transform.TransformPoint(corner);
         var d = Vector2.Distance(test, point);
         if (d < dist) {
             dist = d;
             result = test;
         }    
     }
     return result;
 }
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 Distant-Object · Feb 03, 2014 at 10:55 PM 0
Share

Thank you!

avatar image MarcioLiao · Jun 16, 2014 at 07:33 PM 0
Share

And finding the closest point of a 2dboxcollider? do have any suggestion?

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

18 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

Related Questions

code your own colision system i unity 0 Answers

How to keep wheels in place? 1 Answer

Pixel Perfect 2D Polygon Collider 2 Answers

How to convert lot of 2D box colliders into one polygon collider? 2 Answers

I can't get my objects to collide 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