- Home /
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.
Answer by pako · Jun 07, 2015 at 07:48 PM
You can attach a LineRenderer component to an empty GameObject, make a prefab of it, and instantiate it whenever you want:
http://docs.unity3d.com/Manual/class-LineRenderer.html
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:
http://docs.unity3d.com/Manual/class-LineRenderer.html
To detect if the line hits somewhere, you have to use raycast. For 2D:
http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
Answer by crohr · Jun 07, 2015 at 07:43 PM
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. http://docs.unity3d.com/ScriptReference/LineRenderer.html
Your answer
Follow this Question
Related Questions
Change the "down" axis for 2D 2 Answers
how to make 2D physics-based car / motorcycle game? 2 Answers
2D Animation does not start 1 Answer