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 Feref2 · Dec 02, 2019 at 09:19 PM · distance checkclosest

How to find the closest distance between two objects?

One of the objects is always a rotated straight line (a square, really). The other is not constant, but is a character shape. The ColliderDistance2D would work, if it were for 3D. (However, after a test I can verify that the distance is slightly wrong anyway). A raycast or a rb.SweepTest is too expensive, specially since the distance between both shapes can be very large, and also because the whole point of all of this is a broad phase for collision detection.

Comment
Add comment · Show 2
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 Bunny83 · Dec 02, 2019 at 11:02 PM 0
Share

Sorry but this question is too vague. You said you need it in 3d. A line does exist in 3d just as in 2d. A "square" can also exist in 3d however it needs an additional orientation. Also do you really meant square or a rectangular shape? The most important question is however what is the "other" shape? Do you actually talk about the surface of colliders? Or do you mean arbitrary meshes? If you talk about actual colliders, why do you think a sweeptest is too expensive? Unity uses PhysX for 3d physics. It probably doesn't matter what collision detection system you come up with it won't be any faster anyways unless you have some real simplifications. This brings us back to my question again: What is that other shape? What do you mean by "character shape"?


This question need some clarifications. Please edit your question and include more details. $$anonymous$$aybe a screenshot / drawing would help. If your "character shape" is actually a capsule it's kinda trivial.

avatar image Feref2 Bunny83 · Dec 03, 2019 at 05:32 AM 0
Share

It is a 3D rectangular shape, but the collision will always happen below so it can be treated like a line. And is not because PhysX is too slow or anything: am trying to create new physics because that engine doesn´t work for me. I think that sweeptests can be slow because I know they are made with a bunch of raycasts and other collision shapes (the documentation isn´t much more clear); but I know that raycasts are more expensive the longer they are, so they shouldn´t be very good (besides, the whole point of this check is to reduce the amount of sweeptests per frame, otherwise I would be doing one for every object in scene every frame). And the character shape is really just that: a character. I don´t know what needs to be explained there. However, I don´t have the model yet, because am still deciding on how should it look (and am probably not gonna do it until the end of the development). And is not even constant: this distance check also needs to be done with other humanoid forms. So right now, am using capsules to test everything. And I don´t know how to do that "trivial" stuff with capsules. It doesn´t seem that obvious.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Magso · Dec 02, 2019 at 10:38 PM

I found this buried in Unity answers and used it ever since.

 GameObject nearest;
 
 void Start()
     {
     var distance = Mathf.Infinity;
     var position = transform.position;
     var objects = GameObject.FindGameObjectsWithTag("Tag");
     
     foreach(GameObject thisObject in objects)
     {
         var diff = (thisObject.transform.position - position);
         var curDistance = diff.sqrMagnitude; 
         if(curDistance < distance)
         {
             nearest = thisObject.gameObject;
             distance = curDistance;
         }
     }
 }

It basically starts distance from infinity and checks if the distance of the next object in the array is nearer than distance, if so distance is set to that objects distance and in turn finds the nearest.

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 Feref2 · Dec 03, 2019 at 01:20 AM 0
Share

Thanks for the reply, but this only gives the difference between positions, right? Am looking for the distance between the closest vertices of two specific figures. Your code shows which object from a bunch is the closest to a single object, and gets the distance from the positions (presumably the centers), which is never the closest unless you are comparing points... Is there a way of seeing which is the last vertex in a specific direction and then compare positions to get distance?

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

116 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

Related Questions

How to draw a line from the player to the nearest object? 0 Answers

Get point on surface of Collider2D from inside collider. 1 Answer

inconsistent results calculating the distance moved by an object 1 Answer

Find closest "item" object by NavMesh navigation distance performantly 0 Answers

How do i get my script to work? 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