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 window 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 issue in more detail: enter image description here

How can I accomplish this?

Thank you for your help in advanced!

  • \$\begingroup\$ What can you tell us about the shape of the origin, window, and target zones? Any hints about what you're applying this to accomplish, that might help us seed answers in useful directions? \$\endgroup\$
    – DMGregory
    Apr 7, 2020 at 3:18
  • \$\begingroup\$ Origin: a small sphere, like a point. Window: amorphous, but probably a circle or rectangle. Target: looks just like the window. \$\endgroup\$
    – Microbob
    Apr 7, 2020 at 3:19
  • \$\begingroup\$ This is being used in a physics simulation where light rays produced by a mirror (so coming from a reflection in the mirror) is being reflected off of another surface. Light rays of the reflection needs to determine if it can exit the mirror (the window) and then hit the second object. \$\endgroup\$
    – Microbob
    Apr 7, 2020 at 6:03
  • \$\begingroup\$ Ah! So do you need not just one ray that meets this criterion, but a representative sample of satisfying rays, or a distributed coverage over the reachable surface of the second object? \$\endgroup\$
    – DMGregory
    Apr 7, 2020 at 8:56
  • \$\begingroup\$ Just one ray would do. Ideally the algorithm will pick that one ray very well in that it will guarantee a hit unless such hit is impossible. The algorithm should have a “want to accomplish” motive, if that makes sense. Meaning that if the first cast fails, but there is no proof that making a hit is impossible, it should try again and again. Most of the time there’s going to be almost an infinite number of possible hits, just picking one is my problem. \$\endgroup\$
    – Microbob
    Apr 7, 2020 at 15:39


You must log in to answer this question.

Browse other questions tagged .