Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by AxisRob · Nov 18, 2015 at 01:52 PM · vector3distanceprocedural

Calculate a points distance from a vector 3

Essentially, I have the following basic vector calculation:

 var CamTrans = Camera.main.transform;
 var endPos = CamTrans.position + (CamTrans.forward.normalized * renderDistanceInBlocks);
 
 vector3 RenderLine = endPos - camTrans

I'm doing a procedural generation hobby-project, and I want to optimize my engine even further. I am trying to render objects from a grid of candidates based mainly on their proximity to the RenderLine. I googled and found some beastly math, but does Unity have a convenient way to calculate the distance of a point from a line so I can simply:

1) Get the above vector

2) Get the distance of every object from that vector (the step I am asking about)

3) draw only objects within x distance of this vector

4) draw the rest.

I can easily get three and four down, but two is tricky.

Thanks for reading this far, and if the answer is already here somewhere (I searched previous questions as well), please copy-past the link for an easy accepted answer.

EDIT: Terrifying Reference Math

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
0

Answer by ijidau · Nov 18, 2015 at 01:55 PM

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

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 Bonfire-Boy · Nov 18, 2015 at 02:08 PM 0
Share

That's for the distance between 2 points, not the distance between a point and a line.

It only takes 2 points as parameters, and that's not enough to define a point and a line.

avatar image Dave-Carlile Bonfire-Boy · Nov 18, 2015 at 02:27 PM 0
Share

To be fair, the question is asking for the distance between two vectors, even though that's not what OP means or wants.

avatar image Bonfire-Boy Dave-Carlile · Nov 18, 2015 at 02:40 PM 0
Share

Yeah, fair point.

avatar image
0

Answer by Bonfire-Boy · Nov 18, 2015 at 02:16 PM

The first thing to note is that the Vector3 you have constructed, in RenderLine = camTrans - endPos, does not actually represent the line joining those 2 points. The subtraction loses the positional information (that's the whole point of subtracting vectors like that). Think of it as "how to get from camTrans to endPos (without needing to know where camTrans is)". So your step 1) is not actually correct. You need to be using all the information (the coordinates of all 3 points) in your calculation.

Anyway, the formula you want can be found here: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line (see under "Line defined by 2 points")

The maths isn't too beastly, IMO. You know your P0, P1 and (x0, y0), just plug in the values.

Or you could use the formula for d at the bottom of the Wolfram page you linked to. You just need to convert using "A x B" becomes Vector3.Cross(A, B) and "|A|" becomes A.magnitude

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

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

Vector3.Distance returning weird distances? 1 Answer

InverseTransformPoint() help 0 Answers

Things that I don't get in Procedural Cave generation Tutorial 1 Answer

vector3 distance same script different objects 0 Answers

Dot Product not equals 1 0 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