Auto-generate optimal direction for Raycast with specifications


How can I have a Physics.Raycast auto-generate a direction for itself to try to hit a given target through a restricted window of space?


After much work, my problem has slightly morphed into one about mesh colliders: given a (convex) mesh collider, how can I detect all of the points intersecting with the collider? Or, if this can be done easily, can I get one vector3 per object that is within the mesh collider?


This is kind of a long question, so I’ll try to explain it the best I can:

  1. I have 3 GameObjects: a source, a target, and a “window”. This window sits between the source and target.
  2. I want the source to make a raycast that points in the direction of the target (not necessarily it’s transform.position, just a direction that will make it hit the target) and this raycast must pass through (collide and continue through) the windows GameObject
  3. I would like to automatically generate this direction for the raycast to follow such that #2 happens
  4. I would like this algorithm to also recognize if the above is not possible
  5. As an added item, I would like this raycast to pass through (collide and continue through) any objects that may be in it’s way between it and the window

Here’s an image describing my problem:

How can I accomplish this?

Thank you for your help in advanced!

HI @kyangeastsideprep, interesting problem! Are you working in 2D or 3D? What rotation can you expect the window to have? These issues will affect the complexity of algorithm you create.

Work it in 2D first, and I think you will see how to extend it to 3D. Also let’s say the window will not be below the ground, nor rotated - take the window rotation case that you present in your image

Start by casting a ray to some object. You can easily determine if the hit object is in front of a window or behind a window by comparing it with the distance to the window. So in the hit object array, you ignore any that are closer than the window is.

(Or maybe you want to stop there because you know the object blocks the view? Or maybe you want to save those to determine in the next step if they block the view through the window? there are ways you can jigger the method - maybe check for the farthest hit object first… I didn’t quite get exactly what you want to do in your description, but I think you can understand the general idea I am following. ).

Next, you want to know if any objects hit behind the window are within the window view. Conceptually, do this you need to get the angles between caster and the points at bottom and top of the window, and compare that with the hit object ray angle. If the ray is between those two angles, then you know you can “see” the object through the window.

I say ‘conceptually’, because there may be ways other than actually calculating angles that are more elegant, perhaps some vector or quaternion operations - you’d have to try to work through the necessary calculations to discover the best method of determining whether or not the ray cast went through the window opening.