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 calvintmoss · Aug 31, 2014 at 05:55 AM · vector3gravitydistanceradius

Distance between 2 objects exteriors?

So I am trying to setup some planets / space ship system, but I am running into an issue. My code checks if the spaceship is in range of the planet it is effected by the gravity. However currently I am calculating the distance by

 float dist = Vector3.Distance(planet.transform.position, transform.position);

There is an issue with this, in this screenshot you can see here http://puu.sh/bePps/14841e0600.png the square space ship is effected by the smaller planets gravity but not the larger one. I am assuming this is happening because it is calculating the distance between the spaceship and the center of the planet, not the edge of the planet (like I want)

How can I calculate the distance between the exterior of 2 objects? or at least from the exterior of the planet, I assume it would be something like.

 float dist = Vector3.Distance(planet.transform.position+ distanceToExterior, transform.position);

Currently I am making my spheres larger by scaling their transform in the game editor (not sure if that is important)

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 AlwaysSunny · Aug 31, 2014 at 07:04 AM

Technically, gravity shouldn't care about the surface of an object; only its mass.

The distance to the exterior of a sphere is its radius. Assuming you're scaling it on all 3 axis equally, that'll be any of the scale components divided by two. (or multiplied by one half, which is cheaper to compute)

 (mySphere.localScale.x * 0.5f)

If you wind up using a sphere model whose radius is NOT 0.5, you'll need some extra math. The other possible method involves casting a ray at the surface, which can provide a hit.point and hit.distance, among other things.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Make Navmesh agent follow closest object with tag 0 Answers

Vector3.Distance error plizzzzzz help 2 Answers

Calculate shortest distance between two objects 1 Answer

Find gameobject furthest from player along z axis 0 Answers

Character Controller Not Allowing Gravity 4 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