- Home /
How to find screen pixel world object coord drawn in?
I'm attempting to map an object serving as a 3D cursor back to a screen position where coordinate 0,0 on the 3D cursor object is the "hotpoint" I want mapped.
I would have thought that this would do it:
public Vector2 GetScreenPosition(){
Vector3 screenPos =
Camera.main.WorldToScreenPoint(transform.position);
return screenPos;
}
But I am getting a drift. It is correct at screen center but not as I move away from center, as evidenced by this code:
void OnGUI() {
Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
GUI.Box(new Rect(screenPos.x-5,(Screen.height-screenPos.y)-5,
11,11),"X");
}
Is there some other transformation involved in that that I am missing?
Addl info: There seems to be little to no drift when a flat cursor is drawn at the clipping plane, but the further back in the scene the cursor is drawn, the more pronounced the drift is.
This says to me that WorldToScreen is not correctly figuring for perspective correction?
Answer by wibble82 · Mar 06, 2014 at 06:04 PM
Hi there
That code works perfectly for me (I tried your OnGUI function in my code).
Are you sure you aren't just getting confused about where the transform.position is?
If you picture your object as having a centre point (transform.position), and the front surface (that you can actually see), which is probably a bit closer to the camera (unless your object is totally flat). When the object is dead centre in screen space, its centre point will also appear right in the middle of its front surface.
However, with a perspective camera, as the object moves closer to the edge of the screen its centre point will stop appearing to be at the centre of its front surface. Imagine taking a 3D line from the centre of your object to the exact position of the camera. Your X is being drawn where that lines starts, but where the line intersects the surface of your object will only be in the centre when the the object is right in front of the camera.
Try your code with a very flat object and see what happens. If the drift goes away, there's your issue. It can be solved in various ways depending on the effect you want.
Good point and you are almost certainly right.
Which means I need to rethink the 3D cursor... it may need to be a planar billboard rather then a real 3D object...
Spoke too soon, with a flat textured quad, it shows the exact same drift :/
So, if I put the quad right at the clipping plane, it seems to work or at least have so little drift as to be unnoticeable.
But further back in the scene it drifts.
This says to me that WorldToScreenPoint is not calculating in the perspective correction??
Your answer

Follow this Question
Related Questions
WorldToScreenPoint bad return at large values 0 Answers
draw canvas/2D space Box Colliders on top of 3D objects that are located in world space? 0 Answers
convert world to screen (crosshair follow) 1 Answer
HealthBar for 5 Objects 3 Answers
Converting Mouse Position to World. Stationary Camera. 4 Answers