- Home /
How can I draw a line between 2 objects in Unity2D
I have an aiming reticle, and want to draw a line connecting the position of the reticle GameObject to the ball GameObject. I have done a lot of reading and googling and have found people mention Gizmos.DrawLine but I cannot get my head around it and could do with a concrete code example of simply drawing a line between 2 objects!
The Gizmos is an Editor class and therefore will not work at runtime. One solution is the LineRenderer component. You can attach it to a game object with:
Component > Effects > Line Renderer
Can you demonstrate an example robert where you can use this in Unity2D?
It will be a day or so before I get back to my desktop. But did you try a LineRenderer in your 2D scene? As a test, you can just set the start and end positions for a single line in the Inspector.
Yeah i tried it - and it looks like what I will need to use - just not sure how to interact with it correctly from the code.
Answer by drudiverse · Apr 19, 2014 at 04:19 PM
You can just place a line object in the middle of both objects, with direction of object b - a, and length = disntance (b,a).
line cube/line plane object position= lerp(obj1, obj2, o.5),
object lookat = object 2;
object size. z = vector3.distance(obj1,obj2)
it's 3 lines, it's pretty efficient. instead of lookat, you can use object rotation = (obj1 pos - obj2 pos)
Your answer
Follow this Question
Related Questions
Is it possible to make guitexture above gui.drawtexture? 0 Answers
3d Line Drawing in editor with z-sorting 2 Answers
Line Renderer in World Space 2 Answers
How to create a line by mouse but in x and y direction only 0 Answers
How to draw a gizmo on a Canvas ? 2 Answers