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 Vel_1828 · Mar 28, 2015 at 09:28 PM · positiondistancespace

"How far to the local left/right is another object"?

The title pretty much sums it up. I need to inplement a bit of code which will check the distance from object A to object B, but only on one axis: the local x axis of object A.

Image Example of this problem!

I guess the answer is simple, yet I'm a bit confused.

spaaaaaaace.png (30.5 kB)
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

2 Replies

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

Answer by Eno-Khaon · Mar 29, 2015 at 09:34 AM

http://docs.unity3d.com/ScriptReference/Vector3.Project.html

You need to project a vector of (B - A) onto A's X-axis from the direction of (presumably) A's Z-axis as the normal.

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 Vel_1828 · Mar 29, 2015 at 11:11 AM 0
Share

I will try it ASAP when I'm back from travel, thanks!

avatar image Vel_1828 · Mar 31, 2015 at 06:10 AM 0
Share

Ok, I'm back and I seem to miss something. Vector3.Project uses two numbers (Direction and Normal). But this sollution requires three (Direction B-A, A.x and A.z). How should it look in the code?

avatar image Eno-Khaon · Mar 31, 2015 at 08:32 AM 2
Share

Sorry, I suppose I did kind've overstate it.

Vector projection is based on the idea that a vector (i.e. B - A) is projected onto another vector (A's local X-axis). The Z-axis, being perpendicular to the X-axis, would inherently be the part removed by this projection.

For instance:

 // (B - A)
 Vector3 distBetween = (B.transform.position - A.transform.position);
 // A's local X-Axis
 Vector3 xAxis = A.transform.right;
 
 // Vector projection on A's Z-Axis (assu$$anonymous$$g the Y-Axis isn't a factor
 Vector3 xAxisDist = Vector3.Project(distBetween, xAxis);

If the Y-axis is factored in and is not desired, you could ins$$anonymous$$d set the Y value in "distBetween" to 0.

avatar image
1

Answer by Vel_1828 · Mar 31, 2015 at 04:39 PM

Eno Khan's comment, after a few tweaks, works fine!


Vector projection is based on the idea that a vector (i.e. B - A) is projected onto another vector (A's local X-axis). The Z-axis, being perpendicular to the X-axis, would inherently be the part removed by this projection.

For instance:

 // (B - A)
 Vector3 distBetween = (B.transform.position - A.transform.position);
 // A's local X-Axis
 Vector3 xAxis = A.transform.right;
 // Vector projection on A's Z-Axis (assuming the Y-Axis isn't a factor
 Vector3 xAxisDist = Vector3.Project(distBetween, xAxis);
  

If the Y-axis is factored in and is not desired, you could instead set the Y value in "distBetween" to 0.

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

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

Related Questions

Distance along one axis? 1 Answer

Mathf.PingPong from a random position problems 0 Answers

how do i fix my script in the content below? 0 Answers

Render 3D object to a 2D texture at runtime 1 Answer

Best way to find triangle that a given point occupies. 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