Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
6
Question by mcdroid · May 04, 2011 at 11:50 PM · distanceraypoint

distance between a ray and a point

How do I calculate the distance between a ray and a point ?

Comment
Add comment · Show 2
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 Peter G · May 05, 2011 at 12:11 AM 0
Share

Is the point on the ray?

avatar image Pangamini · Mar 19, 2013 at 05:02 PM 0
Share

Then distance would be zero

6 Replies

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

Answer by mcdroid · May 05, 2011 at 01:43 AM

DaveA, it's too complicated here is the solution

 public static float DistanceToLine(Ray ray, Vector3 point)
 {
     return Vector3.Cross(ray.direction, point - ray.origin).magnitude;
 }
             

Comment
Add comment · Show 4 · 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 UltimateBrent · Nov 20, 2011 at 08:59 AM 15
Share

For those curious the actual point of intersection on the ray is: ray.origin + ray.direction * Vector3.Dot(ray.direction, point - ray.origin)

avatar image ThomLaurent UltimateBrent · Nov 24, 2021 at 04:49 PM 0
Share

An alternative using Projection to get intersection point: ray.origin + Vector3.Project(point - ray.origin, ray.direction)

avatar image julkiewicz · Sep 09, 2016 at 11:55 PM 4
Share

For those wondering where this came from, it's the same principle as here:

https://www.youtube.com/watch?v=tYUtWYGUqgw

just in 3d. Also I understand this exploits the fact that ray.direction is always normalized (therefore no need dividing by magnitude of direction, which is = 1 always).

avatar image craig4android · Jun 23, 2019 at 10:36 PM 0
Share

ty you are a lifesaver

avatar image
4

Answer by friuns3 · Aug 25, 2013 at 09:11 PM

   public static float DistancePointLine(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
     {
         return Vector3.Magnitude(ProjectPointLine(point, lineStart, lineEnd) - point);
     }
     public static Vector3 ProjectPointLine(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
     {
         Vector3 rhs = point - lineStart;
         Vector3 vector2 = lineEnd - lineStart;
         float magnitude = vector2.magnitude;
         Vector3 lhs = vector2;
         if (magnitude > 1E-06f)
         {
             lhs = (Vector3)(lhs / magnitude);
         }
         float num2 = Mathf.Clamp(Vector3.Dot(lhs, rhs), 0f, magnitude);
         return (lineStart + ((Vector3)(lhs * num2)));
     }
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 Diablo_sv21 · Jun 14, 2014 at 02:17 AM 0
Share

This is exactly what I needed! Thank you!

avatar image JokerDen · May 05, 2021 at 06:21 PM 0
Share

Thanks! You saved a lot of time. Don't have idea that 1E-06f means :D

avatar image elenzil JokerDen · May 05, 2021 at 06:24 PM 1
Share

1E-06f is scientific-notation shorthand for 0.1 so 0.000001, a tiny number.

avatar image
1

Answer by OR_Parga · Oct 03, 2016 at 08:45 PM

HandleUtility.DistancePointLine

https://docs.unity3d.com/ScriptReference/HandleUtility.DistancePointLine.html

Comment
Add comment · Show 4 · 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 MattLebrao · Apr 28, 2018 at 07:00 AM 0
Share

Simplest answer ever! Hahahaha Thanks a lot! :D

avatar image Bunny83 MattLebrao · Apr 28, 2018 at 07:49 AM 2
Share

This is an editor only solution since the HandleUtility is an editor class which can not be used at runtime (which means you can not use it in your game, only in editor scripts).

avatar image MattLebrao Bunny83 · Apr 28, 2018 at 09:45 AM 0
Share

That's good to know, but for my current problem (which is in an editor script) that will do it :)

avatar image JokerDen · May 05, 2021 at 06:40 PM 0
Share

Sad it working only at Editor and not in Runtime (you can't build the project with usage of UnityEditor class).

avatar image
1

Answer by elenzil · Oct 03, 2016 at 10:26 PM

if your point and line are two-dimensional, i have an extremely efficient routine for calculating the distance between them, documented here.

in general for questions like this, it's hard to beat Paul Bourke.

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
-1

Answer by DaveA · May 05, 2011 at 12:19 AM

http://www.codeguru.com/forum/printthread.php?t=194400

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 TheMightySeaSlug · Feb 23, 2018 at 03:23 PM 0
Share

https://www.thefreedictionary.com/overcomplicate

  • 1
  • 2
  • ›

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

14 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

Related Questions

Find center between 4 points 3 Answers

Shooting at mousePosition 1 Answer

Trying to cast a ray towards an imaginary wall and get a "hit" / "end" point 2 Answers

distance travelled on y axis 2 Answers

Setting a point with a ray C# 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