Guilherme Garcia da Rosa
Guilherme Garcia da Rosa

Reputation: 1030

How to get correct unity main camera dimensions?

As I have known, the correct size for the camera is half of your wanted width, so in my case:

Width: 1920
Height: 1080
Camera Size: 540
Each block represents 64x64 pixels

In unity I was having this problem by start placing blocks in the -960 (half of 1920, which is the camera minimun x): enter image description here Since as shown I thought, just add half of the blocks width to its x, and subtract half of its height in its y, so this happen: enter image description here After a lots of tries I figured out it was supposed to be placed at -938 and 518, which is 22 units away from the corner, by placing in that position this is my result: enter image description here (Ignore the other blocks for now, they are with wrong algorithm now)

So what I ask is: Why 22? How can I get to that value from my starting values? (Please don't answer something like 2.032%, because probably have something more integer to its math calculation)

Code if needed:

        float height = Camera.main.orthographicSize *2f;
        float width = height / (float)Screen.height * (float)Screen.width;
        Destroy(map);
        map = new GameObject();
        print("W: "+width+". H: "+height);
        lastH=height;
        lastW=width;
        for (int i=0; i<30; i++) {
            for (int j=0; j<16; j++) {
                if(mapa[i,j]>0){
                    GameObject aux = objetos[mapa[i,j]-1];
                    float wX=aux.transform.localScale.x,hY=aux.transform.localScale.y;
                    print ("wX: "+wX+", hY: "+hY);
                    float unidadeW = 2*(float)lastW/(float)(20*wX);
                    float unidadeH = 2*(float)lastH/(float)(20*hY);
                    //aux.transform.localScale = new Vector3(unidadeW,unidadeH,0);
                    GameObject t = (GameObject)Instantiate(aux, new Vector2(j*wX*wX/100-width/2+wX*wX/200,-i*hY*hY/100+height/2-hY*hY/200),Quaternion.identity);
                    t.transform.parent = map.transform;
                }
            }
        }

Upvotes: 0

Views: 2320

Answers (1)

Guilherme Garcia da Rosa
Guilherme Garcia da Rosa

Reputation: 1030

The correct way to make a distance between two units is their diagonal, see the image below: enter image description here

Doesn't matter which is your orientation point, if its top left or center, the distance between the blocks will be √2*side, so the correct distance between mine was around 22, and I can find it by doing this equation:

sqrt(2)/2*32=22.62 (the correct distance between two tiles of 64x64), instead of using 64+ at the last x and 64+ at the last y the right way to do is by adding 22.62 at both multiplications.

Upvotes: 1

Related Questions