How to draw line in the game, not editor.

Hi,

I’m interested to know how to draw a line in the game, not the in the editor and Debug Draw Line and such.

I need to find out if that line intersects with objects so I can reflect it upon the collision point. Imagine that I want to make a 2D game that there is a flash light and we can see the light beam coming out of it and when it hits a mirror, it gets reflected and so on.

Thanks.

I would use a LineRenderer for the visuals but I believe to find the length and reflection point you would need to use a Raycast. Anyway here is the documentation for the LineRenderer.

You can attach a LineRenderer component to an empty GameObject, make a prefab of it, and instantiate it whenever you want:

This will draw a line for you, and if you want to use it in 2D, you can have z=0 for both start and end points.

This is the script interface for this component:

To detect if the line hits somewhere, you have to use raycast. For 2D: