Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 kliddm · Mar 26 at 11:47 AM · mathtrigonometry

How to find a biggest gap between objects?

Hey guys. So I have a player and objects around him with random x-z position. The goal is to find the best way in between those objects (yellow line). The best way means to go trough the middle point between two farthest towers (green line).

The question is - how do I script this? How can I find this gap between farthest objects (green line)?

I already tried two approaches: 1. I found all possible pairs of objects and distance between them, I picked the biggest distance, but it is not giving me the gap I need. 2. I tried to found closest object for each object in the scene, then picked the biggest distance, but it was not working for the player perspective.

Any ideas how can I crack it out please?

Again - I need to find a green line (biggest gap between two objects) from player perspective.

alt text

capture.png (13.5 kB)
Comment
Add comment · Show 1
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 elenzil · Mar 26 at 09:24 PM 0
Share

I think you may want to work on clarifying your criteria. In your picture, I see many pairs of red dots with greater separation than the one chosen.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rh_galaxy · Mar 26 at 03:02 PM

The path you are trying to find is neither the closest distance between towers beginning with the closest tower or the farthest distance. Maybe you want some point along the current path. Without counting for the current path/direction it is not possible to find the line you want, because it could just as well be straight down in your example.

Here is a method that might help you, but i'm not sure.

 static public float PointDistanceToLineSeg(Vector2 p, Vector2 l0, Vector2 l1/*, out Vector2 o_vClosest*/)
 {
     Vector2 l0_l1;
     float dist;
     l0_l1 = l1 - l0;
     float l2 = (l0_l1.x * l0_l1.x + l0_l1.y * l0_l1.y); // i.e. |l0-l1|^2 -  avoid a sqrt
     if (l2 < 0.01)
     {
         //just take l0, the line is too short
         dist = PointDistance(l0, p);
         //if (o_vClosest!=null) o_vClosest = l0;
     }
     else
     {
         //consider the line extending the segment, parameterized as l0 + t (l1 - l0).
         //we find projection of point p onto the line. 
         //it falls where t = [(p-l0) . (l1-l0)] / |l1-l0|^2
         float t = Vector2.Dot(p - l0, l0_l1) / l2;
         if (t < 0.0)
         {
             //beyond the 'l0' end of the segment
             dist = PointDistance(l0, p);
             //if (o_vClosest!=null) o_vClosest = l0;
         }
         else if (t > 1.0f)
         {
             //beyond the 'l1' end of the segment
             dist = PointDistance(l1, p);
             //if (o_vClosest!=null) o_vClosest = l1;
         }
         else
         {
             //projection falls on the segment
             Vector2 projection = l0 + (l0_l1 * t);
             dist = PointDistance(p, projection);
             //if (o_vClosest!=null) o_vClosest = projection;
         }
     }
     return dist;
 }
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

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

Define circle using 3 points 0 Answers

How can I launch a ball given an angle in degrees and a magnitude? 2 Answers

Math question, angles and magnitude stuff 2 Answers

clamp rotation by mouse direction 0 Answers

Mid point of a triangle 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