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 /
  • Help Room /
avatar image
0
Question by Raresh · Dec 08, 2015 at 07:50 PM · c#distance

Closest object inside Collider?

alt text

 To make it easier, I'm going to illustrate them on the same plane
 Big purple circle = Object of interest
 Bigger pink circle = Trigger collider
 Green circle = Object with a Trigger collider and kinematic rigidbody

I would need for the big purple circle to know which one of the green objects is the closest. I am using an OnTriggerStay, but as I noticed it does not return all the objects inside the trigger, and so I can't use a foreach to iterate through them. How would I need to go about calculating the distance only once, and not for each OnTriggerStay call? And OnTriggerEnter would not work because once I compute the closest target I move to it then that object is destroyed, then repeat the process. Hope this make sense. Thanks for any help

untitled.png (318.6 kB)
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 Fanttum · Dec 08, 2015 at 09:35 PM 0
Share

You might just have to reference all the green dots and find their distance to dark purple. Not sure if the triggers are helping you here at all.

avatar image Raresh · Dec 08, 2015 at 09:40 PM 0
Share

It is helping me, because I have hundreds of the green ones and like ten times less purple ones. They do what they are supposed to at the moment, but they don't go to the closest green circles. And calculating the distance for each green dot will be a way too high complexity for what I would need... I just need a way to iterate through the triggers, just like you would do with colliders with a foreach.

2 Replies

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

Answer by Yword · Dec 09, 2015 at 02:51 AM

Maybe you can try Physics.OverlapSphere.

 Collider[] colliders = Physics.OverlapSphere(center, radius);
 
 Collider nearestCollider = null;
 float minSqrDistance = Mathf.Infinity;
 
 for (int i = 0; i < colliders.Length; i++)
 {
     float sqrDistanceToCenter = (center - colliders[i].transform.position).sqrMagnitude;
 
     if (sqrDistanceToCenter < minSqrDistance)
     {
         minSqrDistance = sqrDistanceToCenter;
         nearestCollider = colliders[i];
     }
 }


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 Raresh · Dec 09, 2015 at 12:58 PM 0
Share

With a few tweaks I made it work like i wanted to, thanks

avatar image
0

Answer by Adam_Benko · Jan 17, 2019 at 02:19 PM

Hi, how does your script look like ? I am using OnTriggerStay for automatic turret too. It would help me a lot, thanks :)

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

35 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

Related Questions

How I can make my camera keep a distance between two objects? 0 Answers

Modify Draw Distance via Script? 1 Answer

Help with distance between two objects 1 Answer

look at closer 1 Answer

Measure between 2 Objects & show in UI 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