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 Ekta-Mehta-D · Mar 20, 2013 at 07:29 AM · gameobjectdistance

Calculate Distance Between two Game objects

Hello I am here finding group of sphere of same tag..

For that oncollision , i have got array of all the sphere which are in contact of collided sphere..

Method i have used : var hitColliders = Physics.OverlapSphere(collision.transform.position, 0.6);

By this method , I am getting group of sphere connected to each other. But i want to find the closest sphere among this array. So i need to calculate the distance between bullet sphere and all the game objects within this array. From that i want to find minimun distance object.

But i am not getting the way to calculate distance?? Please Help me out. Thanks in advance for ur support and help..

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 Fattie · Mar 20, 2013 at 07:40 AM 2
Share

type "Distance" in t the documentation

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by sparkzbarca · Mar 20, 2013 at 07:31 AM

vector3.distance(sphere1.transform.position,sphere2.transform.positon)

mark as answered and have a nice day

Comment
Add comment · Show 3 · 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 sparkzbarca · Mar 20, 2013 at 07:38 AM 0
Share

basically you'll just want to

 closestcollider = null;
 distance = 999f;
 foreach (collider in array)
 {
 if(vector3.distance(collider.transform.position,bullet.transform.position) < distance){
 distance = vector3.distance(collider.transform.position,bullet.transform.position);
 closestcollider = collider;
 }

you'll go through the list and any time you find a collider closer than any you've found so far you'll change the distance so you know the current closest distance and change the closestcollider so you have the current closest collider. at the end you have the closest collider as well as how close it is.

avatar image Ekta-Mehta-D · Mar 20, 2013 at 07:49 AM 0
Share

Oh Thanks.. :-)

avatar image fafase · Mar 20, 2013 at 09:29 AM 2
Share

I ummarked it as answer for two reasons, first it is not vector3.distance but Vector3.Distance().

Second because Distance is to be avoided if you want to optimize your game (which you should want to). Distance is based on square root which is a recursive function and can slow down if used every frame on many objects.

Ins$$anonymous$$d you may use:

 Vector3 distanceSqr = (target.position - player.position).sqr$$anonymous$$agnitude;

 if(distanceSqr < rangeSqr) //Within range

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

13 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

Related Questions

A node in a childnode? 1 Answer

How to scale an object between two values over distance 1 Answer

iTween MoveTo "Convoy" 0 Answers

How to set image in Aspect fit on Texture2D 0 Answers

grab all game objects with a distance of the object? 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