- Home /
Position 3d objects in 2d space (orthographic cam)
Hello,
I am using sprites and an orthographic camera, I have a sprite as a background and other sprites as animated object. I need to position the objects in the 2d space. To be more clear, consider a box (GUI.Drawtexture) at center, you could have Rect(Screen.width/4 , Screen.height/2 , Screen.width/2 , Screen.height/3); you can do anything with Screen.width and Screen.height.. I need to know what to do with the sprites. I tried a method:
height = Camera.mainCamera.orthographicSize*2;
width = (Screen.width / Screen.height) * height;
I am printing the values on the screen and I got 0 for width ,2 for height on the ipad, and 4 for width and 2 for height when ran on editor.. I can't figure out what these numbers means.. Is there an another method?
Answer by robertbu · Apr 26, 2013 at 01:24 PM
There are four coordinates systems used by Unity: GUI, Viewport, Screen, and World. There are ways to convert between them. The GUI.DrawTexture() uses GUI coordinates. The width/height are distances in world space, but don't represent coordinates at all. How to solve your problem will depend on what you are trying to do. To make the kinds of positioning you describe with GUI.DrawTexture using an object that lives in world space, you can use Viewport coordinates and convert them to world coordinates using Camera.ViewportToWorldPoint(). Viewport coordinates start at (0,0) in the bottom left and go to (1,1) in the upper right. So to positions something 1/4 up from the bottom and 3/4 the way across the screen:
var v3Pos = Vector3(0.25, .75, 10);
v3Pos = Camera.main.ViewportToWorldPoint(v3Pos);
Note that the 'Z' parameter of the Vector3 is the distance in front of the camera which is more of a concern for a perspective camera than an orthographic camera.
Your answer
![](https://koobas.hobune.stream/wayback/20220613101022im_/https://answers.unity.com/themes/thub/images/avi.jpg)