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 TifaOng · Nov 04, 2013 at 06:55 AM · c#math

Shortest distance from a point to a vector

I have a ball velocity (wihout end point) and a player. I want to calculate the shortest distance (perpendicular distance) between the player and the ball velocity/direction.

I know it uses dot product, but I need further explanation as other sites do not have what I want.

Thanks!

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
4
Best Answer

Answer by Tomer-Barkan · Nov 04, 2013 at 07:29 AM

You can't calculate shortest distance from a point to a vector. You can calculate distance from a point to a line (Ray in unity), since a vector denotes either direction or position, but not both at the same time. So any vector can basically pass through any point, the distance being zero.

If you want to find the distance between a point and a line, you can find your answer here.

To sum it up:

 Vector3 direction = rigidbody.velocity.normalized;
 Vector3 startingPoint = transform.position;
 
 Ray ray = new Ray(startingPoint, direction);
 float distance = Vector3.Cross(ray.direction, point - ray.origin).magnitude;
 


Edit: Forgot to normalize the velocity.

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 Jamora · Nov 04, 2013 at 07:40 AM 0
Share

Vectors can be used to denote a point. The vector (1,1,1) is the point you reach when you move 1 along each axis in a 3D space.

But in a strict mathematical sense you are correct, vectors are only direction.

avatar image Tomer-Barkan · Nov 04, 2013 at 07:43 AM 0
Share

You're right, they're either points or directions, but they can't denote a line (which is a combination of point and direction), unless you assume the line passes through 0,0,0 (in which case you have a point...)

avatar image lucasmontec · Aug 26, 2017 at 02:32 AM 0
Share

Actually (sorry to revive this but misinformation is bad), you can use two vectors to represent a line. Vectors are just structs with 3 floats and a bunch of methods. You can use one vector as a point in space and another as its direction. There you have it. A line.

avatar image apkdev · May 01, 2018 at 07:40 PM 3
Share

You're creating a ray using the origin and direction constructor, only to use it to access the origin and direction vectors you already had before. Additionally, you're unnecessarily normalizing the direction vector - the Ray constructor does this for you.

avatar image
2

Answer by blaskojoz · Oct 20, 2020 at 12:28 PM

If you are looking for shortest path from point to line segment you can use: HandleUtility.DistancePointLine

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 Bunny83 · Oct 20, 2020 at 04:19 PM 1
Share

While this is correct, please note that this is an editor only method since it's part of the UnityEditor namespace. So it can not be used in your runtime code / game, only in editor tools.

Though the code is quite trivial anyways.

avatar image Fenikkel Bunny83 · Oct 29, 2020 at 11:56 AM 0
Share

Quite trivial but you saved my ass

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Clarification on how RangeAttribute works 1 Answer

Am I missing something when it comes to basic math? 1 Answer

Player rapidly accelerating instead of stopping 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