- Home /
Unity C#: Line renderer from Gameobject (3D) to Canvas (Screen Space - Camera)
I have a 3D object in my scene and i want to draw a line(LineRenderer) between the 3D object and one Canvas that have rendering mode Screen Space -Camera.
Destination = GameObject.Find("MyCanvas").GetComponent<Canvas>().transform;
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
lineRenderer.SetColors(laserColor,laserColor);
lineRenderer.SetWidth(initW,finalW);
lineRenderer.SetVertexCount(2);
Origin= GameObject.Find("MyObj");
lineRenderer.SetPosition(0, Origin.position);
lineRenderer.SetPosition(1, Destination.position);
This code works well if the objects are two 3D objects. In my case the line between the 3D object and the Canvas is wrong. The line is going to wrong coordinates from the 3D object. This is obviuosly a coordinate problem. How can i get the right coordinates from my Canvas? There is some kind of conversion i have to do? Thanks for the help.
Answer by Just_Jim_98 · Mar 19, 2019 at 11:53 AM
instead of trying to find the world position of the canvas object, how about you try using Camera.ScreenToWorldPoint() this way you pass it a position on the screen and it will convert that into a world position.