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
3
Question by OverDavide · Mar 24, 2015 at 06:06 AM · collidersdistanceboxcollider

Calculate Distance between 2 colliders

I'm trying, unsuccessfully, to find a way to easily calculate the distance between two objects's collider. In my case I'm trying between two box collider,

             BoxCollider    boxColStart = start.GetComponent<BoxCollider>();
             Vector3 centerBoxStart = boxColStart.ClosestPointOnBounds(end.transform.position);
 
             BoxCollider    boxColEnd = end.GetComponent<BoxCollider>();
             Vector3 centerBoxEnd =  boxColEnd.ClosestPointOnBounds(start.transform.position);
         
             lineRenderer.SetPosition (0,  centerBoxStart);
             lineRenderer.SetPosition (1,  centerBoxEnd);



the code I'm using is wrong and this sketch is the proof:alt text

every suggestion is very welcome

Thanks

Davide

schermata-2015-03-23-alle-164402-copy.jpg (76.9 kB)
Comment
Add comment · Show 6
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 termway · Mar 24, 2015 at 01:23 PM 0
Share

You should try Physic.Raycast from start to end and from end to start (http://docs.unity3d.com/ScriptReference/Physics.Raycast.html)

avatar image OverDavide · Mar 24, 2015 at 01:59 PM 0
Share

you may be more explicative such as the steps to do? :)

avatar image termway · Mar 24, 2015 at 02:11 PM 0
Share

Use a raycast from start object to end object to get the hit collision point on end face object. Use an another raycast from end object to start object to get the hit collision point on start face object. Compute the distance between the two hit point. It won't give you exact result but it should give you a more precise distance. If it's not enough http://gamedev.stackexchange.com/questions/6750/easiest-way-to-compute-closest-point-between-two-triangular-meshes

avatar image siaran · Mar 24, 2015 at 02:53 PM 0
Share

$$anonymous$$eep doing closestpointonbounds with the results you get from doing that until the closest points no longer change?

avatar image hexagonius · Mar 24, 2015 at 03:20 PM 0
Share

If they don't rotate, use their positions, their bounds extends and calculate the distance with them. If they rotate, watermy's suggestion is probably the easiest.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Ericool · Mar 24, 2015 at 04:14 PM

look in collider.bounds.center instead it gives a vector3

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
avatar image
0

Answer by mojtaba1995 · Aug 05, 2016 at 04:29 PM

If your collider is set on your mesh you can find the distance between meshes using vertices and etc. see link below for more (first one is for 2d meshes and the next is for 3d ) https://www.youtube.com/watch?v=R_kV3YiJqEw // https://www.youtube.com/watch?v=642o94MNfs4

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

horse with rigid body and boxcollider wont collide with anything i tried mesh collider with and without convex , i tried box collider with and without rigid body. 2 Answers

i wanna prevent my collider from falling down flat when climbing slope 0 Answers

Hey guys, can you help me out with my colliders. 0 Answers

How come FPS charcter ignores any possible collider? 3 Answers

Unity detecting collision without touching 1 Answer


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