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 burkleypatterson · May 27, 2011 at 07:03 PM · objectgravityspherenearest

Find nearest object using Physics.OverlapSphere

I am making a game that uses planetary gravity and I want the player to be able to use a kind of "augmented gravity." That is, I only want gravity from the nearest planet to act on the player. To do this I want to make a function that sorts through the objects returned by Physics.OverlapSphere for the nearest one so my gravity script only applies gravity from the nearest planet. Does anyone have such a sorting function I can use? I've tried doing it myself, but I'm having some trouble with using arrays.

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 flaviusxvii · May 27, 2011 at 07:06 PM 0
Share

So you're using OverlapSphere.. centered on the player? With a radius equal to the distance to the farthest planet? How many planets?

avatar image burkleypatterson · May 27, 2011 at 07:23 PM 0
Share

yes. the sphere is centered on the player and the radius is an arbitrary number I chose because I knew would include all the planets. The game play shouldn't require more than ten or so planets to be active at a time. to clarify, I have my normal gravity script working. It just loops over all the sphere collisions with the planets and applies a corresponding force to the player. I'm having trouble modifying this to only apply force from the nearest planet.

2 Replies

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

Answer by iggy · May 28, 2011 at 10:50 AM

you could find a nearest planet by searching for smallest distance between planets and player.

C# code

 GameObject nearestPlanet;
 float nearestDistance=float.MaxValue;
 float distance;
 
  foreach(GameObject planet in planets) {
         distance = (player.transform.position, planet.transform.position).sqrMagnitude;;
         if (distance<nearestDistance) {
               nearestDistance=distance;
              nearestPlanet=planet;
         }
     }
Comment
Add comment · Show 7 · 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 Bunny83 · May 28, 2011 at 11:58 AM 1
Share

Nice mix of C# and JS ;)
After converting your script to one language it should work. But it's better to use sqr$$anonymous$$agnitude ins$$anonymous$$d of magnitude/Vector3.Distance. Vector3.Distance calculates the correct distance but it needs a squareroot. If you just want to compare distances you can use the squared distance.

 foreach(GameObject planet in planets) {
     distance = (player.transform.position - planet.transform.position).sqr$$anonymous$$agnitude;
     [...]
avatar image iggy · May 28, 2011 at 12:02 PM 0
Share

oh thanks :) edited the mix part

anyway it doesn't need squareroot when comparing the distances.

avatar image Joshua · May 28, 2011 at 12:07 PM 1
Share

Doesn't 'need' it, but would make it faster. Also, if you use this more then once you'll have to reset nearerDistance to zero.

avatar image iggy · May 28, 2011 at 12:10 PM 0
Share

nearestDistance is reseted to $$anonymous$$axValue.

i didn't understand how would square rooting it make it faster? what am i missing?

avatar image Joshua · May 28, 2011 at 12:21 PM 1
Share

http://unity3d.com/support/documentation/ScriptReference/Vector3-sqr$$anonymous$$agnitude.html "Calculating the squared magnitude ins$$anonymous$$d of the magnitude is much faster. Often if you are comparing magnitudes of two vectors you can just compare their squared magnitudes." Since you don't know the ammount planet in planets will return, this might make quite a bit difference. ;)

Show more comments
avatar image
0

Answer by burkleypatterson · May 28, 2011 at 01:35 PM

oh... that's a much more elegant solution than I had attempted. Thanks a lot everyone!

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

2 People are following this question.

avatar image avatar image

Related Questions

slide sphere on a slope without rotating 1 Answer

How to change use transform.LookAt to make the bottom of an object look at something? 0 Answers

Sphere is noticeably not a sphere when dealing with large spheres? 1 Answer

TriggerEnter, useGravity, Cubes are not falling down 3 Answers

How can I achieve inverse sphere gravity? 0 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