- Home /
Viewport to World Coordinates
Hi guys, I've been looking for similar questions but I couldn't find what I was looking for.
Lets say I have a scene like this.
And I have two gameobjects, the left black dot being at x position -10 While the right black dot being at X position 10.
I want the screen width to return approximately 40 in this case. If i were to lower the screen width to here
I would want the screen width to return approximately 20. I've seen the methods ViewportToWorldPoint and ScreenToWorldPoint but I don't think they can solve my problem here (i may be totally wrong). Any suggestion guys?
Well, since you can't edit what screen.width retuns, then you need to move either camera or dots or both of them to fit situation you want. screen.width will always return you a non-editable value, the width of game window.
Answer by Ezwrath · Jul 03, 2015 at 10:13 PM
Someone else was able to answer on reddit To clarify I wanted the X coordinates for the left and rightmost visible pixels on the screen.
//lowerLeft.x will be the X coordinate of the leftmost visible pixel
Vector3 lowerLeft=camera.ViewportToWorldPoint(new Vector3(0,0,0));
//lowerRight.x will be the X coordinate of the rightmost visible pixel
Vector3 lowerRight=camera.ViewportToWorldPoint(new Vector3(1,0,0));
float screenWidthInWorld=(lowerLeft-lowerRight).magnitude;
Only works for orthgraphic but that's what I have.
Answer by malkere · Jul 03, 2015 at 03:02 PM
you want to move your camera around so that the worldcoords visible on the blue plane are always between 20-40?
viewport dimensions are a flat float between 0 and 1 on the camera lens.
screenpoints are pixel based, 0-480, etc.
if you're orthographic you could shoot a screenpointoray out from the left most point of the screen(in world coords) and see where in world coords it hits the blue plane, then do the same on the right (or not if you are always dead center) and adjust the FoV or use trigonometry to find the best location for the camera to be at (on the z axis)
if you're perspective... you can calc the distance to the blue plane, and knowing your FoV and distance to either of the black dots figure out how far you should actually be back using trigonometry and lerp towards the intended number.
Thank you for your input, I am using an Orthographic Camera. The 20-40 is just an example. But I want to return the width from the leftmost part of the screen, to the rightmost part of the screen in terms of world coordinates. The second image would return 20, because the transform.position.x starting on the leftmost part of the screen would be -10, and the transform.position.x on the rightmost part of the screen would be 10. Would I still have to shoot a screenpointoray?
Similar to finding the width of a 2D sprite in the scene by finding their leftmost starting point to their rightmost ending point. http://imgur.com/lychLjZ
Bounds houseBound = GameObject.Find("house4").GetComponent().bounds;
int width = houseBound.max.x - houseBound.$$anonymous$$.x; //width should be 3 -(-3) = 6
I would like to return the distance of the Right Part of the screen - the left part of the screen, in terms of world coordinates.