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 antx · Feb 15, 2013 at 04:51 PM · directiondistanceworldspacez axis

Distance between 2 objects without their Z axis.

I need to find the distance of two objects but ignoring the Z axis in the calculation. Normaly I would just do:

 Vector3 aV = a.transform.position;
 Vector3 bV = b.transform.position;
 aV.z = 0f;
 bV.z = 0f;
 float dist = Vector3.Distance(aV,bV);

But that only works when I want Z to be the WorldSpace Z. My Z should be the local Z of the 2 objects (I make sure in advance that they face the same direction).

Is there maybe already something in Unity that I can use to easily figure that distance out?

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 antx · Feb 15, 2013 at 05:49 PM 0
Share

Imagine 2 gears on parallel axises. To check whether they are close enough to interact but not too close that they would intersect (beyond their teeth) I need the distance between them but only on a plane (thir common Z axis). (that they touch in Z direction I make sure via collision (in case you wonder)).

In my example above I just killed the Z value from their transforms position vectors and then measured the distance. But this works only when I asume that they are always aligned to the worldspace grid. Once I turn them this will get wrong results.

I came up with the following so far, but that might not be the perfect way to do it (especially if there maybe is a special Unity way to do this):

 Vector3 dist3 = otherCollider.transform.position - transform.position;
 float rDot = Vector3.Dot(transform.right, dist3);
 float uDot = Vector3.Dot(transform.up, dist3);
 float dist = (float) $$anonymous$$ath.Sqrt((uDot * uDot) + (rDot * rDot));
 Debug.Log ("Distance: " + dist);

1 Reply

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

Answer by Loius · Feb 15, 2013 at 05:29 PM

Move b into a's local space:

Vector3 bInASpace = a.transform.InverseTransformPoint(b.transform.position);

bInASpace.z = 0

Now bInASpace should be your flattened-to-A's-local-XY-plane direction + distance.

Comment
Add comment · Show 3 · 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 antx · Feb 15, 2013 at 11:15 PM 0
Share

Okay, this looks interesting. But I don´t know how I then get the distance out of that. I tried this:

 Vector3 bInASpace = otherCollider.transform.InverseTransformPoint(transform.position);
 bInASpace.z = 0;
 float dist2 = Vector3.Distance(bInASpace, transform.position);
 Debug.Log ("Distance B: " + dist2);

I set my scene up that the objects are at 0,0.5,0 and 0.5,0,0. So they are

squareroot(( 0.5^2 ) + ( 0.5^2 )) =

squareroot( 0.25 + 0.25 ) =

squareroot( 0.5 ) =

0,70710678 apart.

But with the code above I get 0.5 as result. I guess Vector3.Distance(bInASpace, transform.position) is not right here. I think it is still using world coordinates?

avatar image Loius · Feb 16, 2013 at 08:37 AM 0
Share

The vector itself is the distance - that is, dist2 = bInASpace.magnitude

avatar image antx · Feb 16, 2013 at 11:44 PM 0
Share

Aha, that works indeed. Thank you. And Thanks to Louizien.

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

10 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

Related Questions

Getting the distance in a known direction 2 Answers

Distance shader returns strage distance when not at 0,0,0 2 Answers

Rotate object so a specific axis faces another object, while also relative to the parent direction 1 Answer

Shader distance between WorldSpaceCameraPos and Vertex 1 Answer

Getting a position behind a target object (targeted attack follow through) 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