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
1
Question by Exce · Jun 20, 2012 at 07:37 PM · vector3distanceedgebetween

Vector3.Distance edge of model

The problem is simple. I have a 3D model and a cube in my game world. On right mouse click I tell my model to run towards the cube until the Vector3.Distance between the two is less than 1. The problem with this is, it seems the "Distance" is from the center of my model to the center of the cube. So when practicing this with a cube with a large width, my model will continue to run at the cube even though he is already colliding with the edge.

How do I check the distance between the edge of my model and the edge of the cube? My intention is to replace the cube with an enemy at some point, but enemies will be different sizes.

Thank you.

Comment
Add comment · Show 1
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 whydoidoit · Jun 20, 2012 at 07:39 PM 0
Share

Well it would be easier with a radius - you would just take that off the sizes of both the mesh and the cube.

3 Replies

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

Answer by AlucardJay · Jun 25, 2012 at 05:25 AM

I was hoping someone would give a nice 'pro' answer, but this is how I tackled the problem.

  • calculate distance between the center of 2 objects

  • raycast from each object

  • each raycast should hit collider of other object

  • raycast distance should be less than center distance, the difference being the distance between the object's center and the edge of the collider facing the other object

  • calculate both colliders' difference , and subtract them from the center distance

  • value should be distance between the colliders.

Create a new scene, attach the below script to the camera or an empty gameObject. Create 2 cubes, and drag-n-drop them in the script's Inspector ( obj1 and obj2 ). Change the scene view to 'wireframe' so the debug lines can be seen. Hit Play and move the cubes around. The GUI shows the difference between the center's and colliders.

 #pragma strict
 
 public var obj1 : Transform;
 public var obj2 : Transform;
 
 private var centerDiff : float = 0.0; // difference between the centers
 private var colliderDiff : float = 0.0; // difference between the collider edges
 
 private var rayDist1 : float = 0.0;
 private var rayDist2 : float = 0.0;
 
 private var drawlineOffset : Vector3 = Vector3(0, 0.02, 0); // for the drawlines to not render over each other
 
 
 function Update() 
 {
     centerDiff = (obj2.position - obj1.position).magnitude; // distance between 2 objects
     
     var rayHit : RaycastHit;
     
     if (Physics.Raycast( obj1.position, Vector3.zero - (obj1.position - obj2.position).normalized, rayHit) ) 
     {
         Debug.DrawLine(obj1.position, rayHit.point, Color.red);
         rayDist1 = Vector3.Distance(rayHit.point, obj1.position);
     }
     
     if (Physics.Raycast( obj2.position, Vector3.zero - (obj2.position - obj1.position).normalized, rayHit) ) 
     {
         Debug.DrawLine(obj2.position + drawlineOffset, rayHit.point + drawlineOffset, Color.green); // offset so line can be seen
         rayDist2 = Vector3.Distance(rayHit.point, obj2.position);
     }
     
     colliderDiff = centerDiff - ( centerDiff - rayDist1 ) - ( centerDiff - rayDist2 ); // colliderDiff - (collider2 radius) - (collider1 radius)
 }
 
 function OnGUI() 
 {
     GUI.Box(Rect(10, 5, 200, 25), "center Difference = " + centerDiff.ToString() );
     GUI.Box(Rect(10, 35, 200, 25), "collider Difference = " + colliderDiff.ToString() );
 }

(this is the same answer I gave for http://answers.unity3d.com/questions/273365/upper-edge-of-an-object.html , was hoping there was a more 'pro' method someone could provide, but this works while the colliders don't overlap)

alt text


collider diff.png (32.8 kB)
Comment
Add comment · Show 1 · 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 Exce · Jul 01, 2012 at 05:58 AM 0
Share

Thanks so much for the response. Sorry it took so long to respond. I had trouble logging in. I'll give this a shot, it seems like a reasonable solution.

avatar image
6

Answer by Agostino · Nov 13, 2013 at 03:55 PM

Raycasting is far heavier than needed.

If checking distance from the AABB is enough, here is a quicker alternative.

You can calculate the distance between the 2 colliders enclosing the objects using an integrated function.

Here's a general function to find the point of a collider that is closest to a point http://docs.unity3d.com/Documentation/ScriptReference/Collider.ClosestPointOnBounds.html

The trick is:

  1. we start from the positions of the 2 objects

  2. we get the ClosestPointOnBounds of Obj1 collider from Obj2 position

  3. we get the ClosestPointOnBounds of Obj2 collider from Obj1 position

  4. we measure the distance between the 2 ClosestPointOnBounds we got

Just make sure both objects have a Collider component (sphere, box, whatever)

 using UnityEngine;
 using System.Collections;
 
 public class TrueDistance : MonoBehaviour {
     public GameObject otherObject;
    
     Vector3 closestSurfacePoint1;
     Vector3 closestSurfacePoint2;
    
     // Use this for initialization
     void Start () {
         // the surface point of this collider that is closer to the position of the other collider
         closestSurfacePoint1 = collider.ClosestPointOnBounds(otherObject.transform.position);
        
         // the surface point of the other collider that is closer to the position of this collider
         closestSurfacePoint2 = otherObject.collider.ClosestPointOnBounds(transform.position);
        
         // the distance between the surfaces of the 2 colliders
         float surfaceDistance = Vector3.Distance(closestSurfacePoint1, closestSurfacePoint2);
         Debug.Log(surfaceDistance);
     }
    
     // Update is called once per frame
     void Update () {
         // visualize the different distances (debug)
         Debug.DrawLine(transform.position, otherObject.transform.position, Color.yellow);
         Debug.DrawLine(closestSurfacePoint1, closestSurfacePoint2, Color.magenta);
     }
 }
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 Huma · Jun 20, 2012 at 08:26 PM

Maybe Raycast can do the job. Not used it yet, just saw it in a script.

http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

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 Exce · Jun 20, 2012 at 08:35 PM 0
Share

That sounds like it might work. I'll give that a shot.

avatar image sameer · Jun 24, 2012 at 08:05 AM 0
Share

how it will work in RayCast and if i did that with Ray Cast In a direction it wont work only if i made all the cubes in one same direction From The Character

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

6 People are following this question.

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

Related Questions

Distance Variable Won't Change. What's Wrong With My Script? 1 Answer

Best Way To Find Distance 2 Answers

Distance Changing Conversions (1000m = 1km 1200m = 1.2km etc) 3 Answers

How do you find the distance of multiple objects in a array 1 Answer

Check distance between multiple objects 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