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
1
Question by BenDurch · Nov 08, 2015 at 11:07 AM · arrayarraysdistancesurfaceordering

help ordering array for planetary gravity

Planets = GameObject.FindGameObjectsWithTag ("Planet"); Planets = Planets.OrderBy(Enem => Vector2.Distance(transform.position,Enem.transform.position) - Enem.transform.localScale.x/2).ToArray(); closestPlanet= Planets[0];

i'm stumped on this part of my code because it's pourpose is to move an object to the closest planet, but if one planet is very big and a small planet is next to it, while the player is walking on the bigger planet, the smaller planet pulls the player towards it because the player was closer to the center of the smaller planet than to the bigger planet it was on. how do i make it so it orders the array by how close it is to the surface and not to the center?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Asgardr · Nov 08, 2015 at 11:41 AM

If your planets have SphereCollider, you can use SphereCollider.radius and substract it from the distance between you and the center of a planet. As following:

 Planets = GameObject.FindGameObjectsWithTag ("Planet"); 
 Planets = Planets.OrderBy( 
     Enem => Vector2.Distance(transform.position,Enem.transform.position) 
     - Enem.GetComponent<SphereCollider>().radius
 ).ToArray();
 closestPlanet= Planets[0];

As a sidenote, please don't do this every frame. GetComponent could seriously slow down your update loop. I recommend caching that in some way. Also, some other optimizations could be replacing your Distance with a squared Distance

 (obj1.position - obj2.position).sqrMagnitude

This means you don't have to calculate the root of the sqrMagnitude every frame (slow).

Comment
Add comment · Show 2 · 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 BenDurch · Nov 08, 2015 at 04:39 PM 0
Share

that didn't work, i was wontering if there is a solution maybe using linecast or raycast 2D. finding the distance from transform.position to hit point.

avatar image Asgardr BenDurch · Nov 08, 2015 at 06:38 PM 0
Share

What didn't work? If your planets have sphere colliders, it should work. Is there something else that could cause it?

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

34 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

Related Questions

Remove values of one array from another C# 1 Answer

Drag and Drop. Grid Array to save if something is placed not working. Fix?,Drag and Drop Grid array to save the positions of placed Object not working. Fix? 1 Answer

Why i can't read a Variable's Length(Array) 2 Answers

Adding a new GameObject to an Array without overriding the others 0 Answers

Cannot access a array in another script 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