- Home /

# detect Ray Crossing? Intersecting?

I have 2 object that are casting rays, I need a way to know if those rays intersect, and at what point. Here's a birds-eye picture of the situation below. (http://s794.photobucket.com/albums/yy225/JaredShadrak/?action=view¤t=raycollisiondetection.jpg)

I thought about creating a vertical plain along one of the rays but I'm not sure if that's the most effective way.

I thought of another way, but maybe there's a more efficient way. but maybe I could do this:

the units firing the raycast can point it slightly down. eventually both rays will hit the terrain mesh and if they cross, that will be reflected in where they hit the ground. once I have where they hit I can triangulate the coordinates and determine the point at which they crossed each other. dude, that's awesome!

if you know of an easier way, please let me know, I don't wanna program that all.

**Answer** by Random Indie
·
Apr 23, 2010 at 05:29 AM

There are a few ways you could do this.

1) You could use the formula found here that gives the intersection point in 3d, however if you're not familiar with linear algebra it might look a little daunting. (even if you are, it's still messy when expanded). Do a find for "In three dimensions, the algebra becomes more complicated." It's about halfway down the page. of course, there's the issue that you're unlikely to have rays actually intersect, and given floating point errors they may never cross according to the computer.

2) You could ignore one component (say Y) and do a 2d line intersection (also available on the first link). Then once you have the point they intersect on the XZ plane, you could just see how far the two rays (at the intersection in XZ) are from one another given a tolerance. However, if the rays are near vertical this could make the floating point errors blow up so it might be a good idea to ignore X or Z if you're 45 degrees above the horizontal, if that's the case, the idea remains pretty much the same.

Your line/plane intersection would definitely work. All you'd have to do is find the point where the ray and plane intersect, then get the perpendicular distance from the ray (forming the vertical plane) to the intersection point on the plane. This should help. This is a more succinct version of the same thing.

Hope that helped, I'm hopped up on cold medication so I can only hope that made some sense. If not, I can try to clarify. No matter what though you're going to probably have to do some algebra (if that's a problem when I'm not so wobbly from the meds I can help out.)

Cheers

Hey no problem. :)

Like I said if you need any help with the theory feel free to ask.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.