- Home /
converting from screen to world space? it seems broken
i have a rectangle that moves across the screen, i tried to connect the top-left corner of the rectangle to the line..i assumed that only thing i need to do is to convert screen to world space and use it to render line.
here is what i tried:
function OnPostRender(){
CreateLineMaterial();
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
GL.Color(Color(1,1,1,1));
GL.Vertex3(0,0,0);
GL.Vertex3(cam.ScreenToWorldPoint(Vector3(Label.rPosX,Label.rPosY,0)).x,-cam.ScreenToWorldPoint(Vector3(Label.rPosX,Label.rPosY,0)).y,cam.nearClipPlane);
GL.End();
}//END OF ONPOSTRENDER
so coords of the rect are Label.rPosX and Label.rPosY (they are inside class Label) the line should go from origin to the projected screen coords into world space. line renders but it is not connected to the rectangle and it goes all over the place when rectangle moves...
am i doing this right? any ideas?
EDIT:
i was searching through the forums and i found that third parametar should be distance from the camera so i modified the part that doest the rendering but still no luck:
var distanceCamera : float= camScript.currentTarget.position.z-cam.transform.position.z; var screenPoint=Vector3(Label.rPosX,Label.rPosY,distanceCamera);
var worldPoint=cam.ScreenToWorldPoint(screenPoint);
GL.Begin(GL.LINES);
GL.Color(Color(1,1,1,1));
GL.Vertex3(0,0,0);
GL.Vertex3(worldPoint.x,worldPoint.y,worldPoint.z);
GL.End();
Your answer
![](https://koobas.hobune.stream/wayback/20220613072201im_/https://answers.unity.com/themes/thub/images/avi.jpg)