Hi guys, need some help on that:
I got some gameobjects (Its a 3d PlayCard (Aces, Kings, Queens, Jacks…) Its a Textured 3dsMax model sized on: x.0.1 y.0.15 z.0.001
How can I setup the size of this this gameObject to see the top surface on the screen in exactly defined pixels like 100x150
or maybe adjust the camera distance to see the disired size of the gameObject? It would be great if i could resize the object and don’t use the camera distance…
Need also the info about how to set this Object to desired position by code…
thanks in advance
Are you using an orthographic camera or perspective? On a perspective camera, you can use Camera.WorldToScreenPoint.
Using an orthographic camera, what I did was position and size the object while in fullscreen on my computer. Once it was where I wanted it to be, I used the following code in Start and Update (NOTE: My screen resolution height is 910.)
public Camera thisCamera;
private float yOrigin;
void Start () {
yOrigin = transform.position.y;
}
void Update () {
thisCamera.orthographicSize = Screen.height / 910f;
thisCamera.transform.position = new Vector3(transform.position.x, yOrigin - ((Screen.height / 910f)-1), transform.position.z);
}
If you’re using an ortho camera, and want to position things per pixel, you should set the orthographicSize of the ortho camera to half the screen height. This has the effect of having world units be equivalent to pixels. Then you would, either through import settings, or in inspector, bring your local scale up so that the dimensions on your object match the desired pixel size. When your units are in pixels, it makes it easy if you want an object 300 pixels above center screen.myTransform.position = orthoCam.transform.up*300 + orthoCam.transform.position;
If however you are using a perspective camera, you would need to find the pixels per world unit at a specific distance from the camera. Once you have that, you can set the scale of your object by that ratio. See this Unity answer for how to get pixels per unit.
Hay guys! Thans for your answers. I’m using an ortho camera! My first idea was to raycast the object and scale step by step to get the desired size. But I thought its an ugly and cpu intensive solution… But u gave me the right direction. Thanks. Going now to play around with your tips. I’m still learning Unity by doing :)))