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
1
Question by raul corrales · Mar 20, 2011 at 09:44 PM · raycastdistanceedge

I need help about raycast and edges

Hi to al.

How I can know the distance between the RayCastHit and the red edge ? alt text

Thanks!

Comment
Add comment · Show 1
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 Statement · Mar 20, 2011 at 10:46 PM 1
Share

I hope Al reads this so he knows you said hi! :)

2 Replies

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

Answer by Statement · Mar 20, 2011 at 10:43 PM

If you know a little math, you can project the point to the line/ray from point A to point B. Then you can measure the distance of the point and the projected point. It's been a while since I did this myself so I can't guarantee it works. :) It might be so you have to replace x with x - a to get the same reference point.

Basically if you have the points a, b and x:

// The vector from a to b. var ab = b - a;

// The point x projected on ab. var xab = Vector3.Project(x, ab.normalized);

// The distance from x to the line ab. var distance = Vector3.Distance(x, xab);

Edit: If above doesn't work it might have to be like this. Or I just dont know a little math after all ^^

// The vector from a to b. var ab = b - a;

// The vector from a to x. var ax = x - a;

// The point ax projected on ab. var xab = Vector3.Project(ax, ab.normalized);

// The distance from ax to the projected xab on line ab. var distance = Vector3.Distance(ax, xab);

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 Bunny83 · Mar 21, 2011 at 02:37 AM 0
Share

Your second script should work ;). I just forgot about the good old dot-product for a sec. So i come up with the geometric approach. I'm not sure what will involve more calculations but this solution at least looks simpler.

avatar image
2

Answer by Bunny83 · Mar 20, 2011 at 10:13 PM

You can use Unitys Plane-class to setup a plane that goes through your hit point and is perpendicular to your edge. Just use the vector AB as normal vector and your hit point as origin for the plane (it's the first constructor variant).

When you have your plane, just Plane.Raycast along the edge and you will get your point. You can use Ray.GetPoint with the distance you get from the raycast to calculate the intersection point.

Maybe there's a way to get the point via RaycastHit-barycentricCoordinates but i think that's even harder :D.

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

No one has followed this question yet.

Related Questions

Raycast always hit 1 Answer

How to make this script find distance inside radius? 1 Answer

[Closed] Instantiate wall/fence down a raycast from point A to point B? 0 Answers

Calculate where an object is going to land 0 Answers

How can I draw a specific distance with Debug.Drawray 4 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