- Home /
Converting world point to screen point
i am trying to make labels for my objects in the scene. obviously i dont know how to properly convert world to screen coords, they seem to rotate around 3d space but they shouldnt
here is the code i use:
function OnPostRender() { var origin : Vector3=cam.camera.WorldToScreenPoint(cam.targetPosition);
CreateLineMaterial();
lineMaterial.SetPass(0);
GL.Begin( GL.LINES );
GL.Color(Color(1,1,1,1));
GL.Vertex3(cam.targetPosition.x ,cam.targetPosition.y, cam.targetPosition.z );
GL.Vertex3( 1,0, 0 );
GL.End();
}
i know this code isnt good, it was just for testing. i want line to go from the middle of the object (that is targetPosition Vector3) and other point should be, let's say 1 point long left or right from the object position, it should just project to the screen, not in 3D space
EDIT: i tried this, but it didnt get me too far (i used origin variable):
function OnPostRender(){
origin=cam.camera.WorldToViewportPoint(SceneControl.ReturnCameraTargetTransform().position);
Debug.Log(origin.ToString());
CreateLineMaterial();
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
GL.Color(Color(1,1,1,1));
GL.Vertex3(origin.x ,origin.y, 0);
GL.Vertex3(origin.x+0.1 ,origin.y+0.1,0);
GL.End();
}
i simply dont get this.. :( what i am trying to do is to do labels for my objects in the scene. first step would be to get the position of the object and draw the line from that point and point that is offset a little. that worked fine if i used for example
GL.Vertex3(cam.targetPosition.x ,cam.targetPosition.y, cam.targetPosition.z);
GL.Vertex3(cam.targetPosition.x+0.5 ,cam.targetPosition.y+0.5, cam.targetPosition.z);
but it rotated in 3d space so my guess was that i need to project the offset point to viewport of screen space, but every combination i tried didnt work...it is so frustrating...
if i use WorldToScreenPoint i get huge numbers for origin.ToString() so i am using WorldToViewportPoint... this seems to be so simple, but i cant get it to work
Answer by Eric5h5 · Jul 01, 2010 at 08:24 PM
You declared the "origin" variable but you're not doing anything with it.
Answer by gloopy · Oct 06, 2013 at 11:26 AM
I'm not entirely sure of your problem but if it is what I think it is and you are using a static camera or you always want your labels to face the camera I added this to my text labels for items in a script:
function Update () {
transform.rotation = Camera.main.transform.rotation;
}
Now my text labels for items / players / anything always face the camera. I am using a Text Mesh to create labels above my items.
Hope this helps, and apologies if I have misunderstood the issue.
Your answer
![](https://koobas.hobune.stream/wayback/20220613063625im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to render 3d line on top everything else in the scene? 1 Answer
Drawing Lines with AA 1 Answer
Transform's position wherever the mouse is pointing. 2 Answers
how to calculate screen to world point (custom) 0 Answers
Is it possible to enable Screen.sleepTimeout to the screen but not the VR view? 0 Answers