Cut off a ray at the edge of a box

Hi there! I’m working on a topdown game where the player has a line of sight. Certain walls (grey cubes) cut off the sight of the player (red cube) by checking the corners of all BoxCollider2D that overlay the yellow box, raycasting a line (cyan line) next to it to check if there’s something behind and finally connect all the points with a mesh.

Now everything is calculated correctly except when a raycast does not hit anything, like the most right cyan line. My question is, how to I find the point where a raycast exits the edge of the yellow box surrounding the player? I need it to complete the mesh’s shape. The yellow box is NOT a BoxCollider2D but just 4 points surrounding the player and connected by Debug.DrawLine()

Thank you for your time and I hope someone can crack this nut.

5339616--538866--Schermopname (2).png

Use Line Line Intersection
http://wiki.unity3d.com/index.php/3d_Math_functions

or change to a yellow circle and just make the ray distance = circle radius :wink:

1 Like

Aaaaah :smile: I thought I could only do it with a box but as you said it’s easier to actually use a circle. And that is what I did. Now the rays stop at the edge of the circle and all I need to do is make the circle fill the screen :smile: Thank you for your quick answer.

5339736--538878--Schermopname (4).png