Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 AlphaNerd_DE · Sep 19, 2013 at 10:45 AM · c#vector3distancecoordinatesaxe

Calculate shortest distance between two objects

How can I get the distance between two objects when one of them is really big (e.g. a wall) and the other one is kinda small? I am aware of vector3.distance but I can't use that, because it uses the world coordinates. It's like calculating the distance on only one axe, but without knowing the axe.

I hope you can help me :)

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 robertbu · Sep 19, 2013 at 10:59 AM 0
Share

I'm unclear about what you are asking. Given you are talking about a wall, Collider.ClosestPointOnBounds() might be the solution you are looking for..

avatar image fafase · Sep 19, 2013 at 03:07 PM 0
Share

You could also get your vertices of each object and get the closest vertex form object A to position of object B, do the same the other way around and get distance between your two found vertices.

1 Reply

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

Answer by robhuhn · Sep 19, 2013 at 10:50 AM

Sounds like you need a Point-to-plane distance - For that you could use this method (c#)

 float distance = PointToPlaneDistance(smallObj.transform.position, wall.transform.position, wallNormal);

 private float PointToPlaneDistance(Vector3 pointPosition, Vector3 planePosition, Vector3 planeNormal)
 {
     float sb, sn, sd;
     
     sn = -Vector3.Dot(planeNormal, (pointPosition - planePosition));
     sd = Vector3.Dot(planeNormal, planeNormal);
     sb = sn / sd;
     
     Vector3 result = pointPosition + sb * planeNormal;
     return Vector3.Distance(pointPosition, result);
 }
Comment
Add comment · Show 4 · 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 AlphaNerd_DE · Sep 19, 2013 at 12:49 PM 0
Share

Thanks for your help, but what is wall-/planeNormal? Where do I get that 3rd Vector from?

avatar image AlphaNerd_DE · Sep 19, 2013 at 02:22 PM 0
Share

If you meant, that the 3rd Vector should be the 2nd one but normalized, than it is not working. I'm still getting different Values for the distance.

avatar image robhuhn · Sep 19, 2013 at 04:18 PM 0
Share

The normal is not the normalized vector but the normal of the walls surface (surface normal).

alt text

avatar image AlphaNerd_DE · Sep 20, 2013 at 03:35 PM 0
Share

It was a bit complicated to find out how to calculate the normal of the wall, but it worked. Thanks a lot :)

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

16 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

Related Questions

Calculate the distance between an object and my player 1 Answer

Multiple Cars not working 1 Answer

How to get a "Traveled" vector point along a path with some width? 1 Answer

C# Creating a 2d boundary 1 Answer

How can I have a distance between 2 points = 0-1 (min = 0, max = 1) and in between is a decimal? [C#] 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