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
2
Question by Kiloblargh · Nov 07, 2012 at 11:39 PM · collisionraycastmathrayvoxel

How would I find the closest Vector3 point to a given Ray?

I'm trying to do a click-to-move-here script on a dynamic ground surface with no mesh collider. I can narrow down the total number of verts to a manageable set, but then what I want to do is go through and find the closest to the camera-to-click-point ray; how can I do that?

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 Loius · Nov 08, 2012 at 12:54 AM

I detest math but I love logic. This question is about maths logic.
Why you torture me with this question. Why you do. :)

Here's a link to a stupid maths-talk website that explains this: Link!

And here's this.

The goal is to calculate the length of the vector V, where V is perpendicular to your ray X1->X2 and intersects your point X0.

 function DistanceToRay( X0 : Vector3, ray : Ray ) : float {
   var X1 : Vector3 = ray.origin; // get the definition of a line from the ray
   var X2 : Vector3 = ray.origin + ray.direction;
   var X0X1: Vector3 = (X0-X1); 
   var X0X2: Vector3 = (X0-X2); 
  
   return ( Vector3.Cross(X0X1,X0X2).magnitude / (X1-X2).magnitude ); // magic
 }


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
3

Answer by drastick · Nov 08, 2012 at 01:04 AM

Pardon my quick answer but if you want to find the distance of a point from a ray or line... Use the Dot Product! Just subtract your ray origin from the point, dot that vector with the normalized ray vector. That gives you the magnitude of the point projected onto the ray. Multiply that magnitude to scale the normalized ray and add back the ray world origin back to that point. Then calculate the distance between that projected world point onto the ray you just found with the original world point. Do it for all of your test points and take the shortest distance.

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 Loius · Nov 08, 2012 at 01:15 AM 1
Share

I think that this $$anonymous$$aths am the same as my $$anonymous$$aths, but with more better explaineding. If you have problems with my $$anonymous$$aths, try using this instad. (Vector3.Dot(vect1, vect2) is the dot function, btw)

$$anonymous$$aths makes my head hurt. xD

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

11 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

Related Questions

detect Ray Crossing? Intersecting? 1 Answer

making ray ignor certain collider 1 Answer

Raycast for 2d with touch. 0 Answers

How can I determine how many colliders exist along a line? 1 Answer

Casting a Ray in the direction of the movement 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