Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Scale your GameObjects for every Screensize

Discussion in 'Editor & General Support' started by Zorou_, Jan 18, 2020.

  1. Zorou_

    Zorou_

    Joined:
    Jan 5, 2020
    Posts:
    7
    Hay,

    so I got this problem, that my GameObjects (for example my Walls) dont scale with the Screensize of the used Device. I tried to figure it out on my own, but only got to the point to "screenscale" all my UI elements. The problem: I want my walls to be on the edges of every device. When testing with an Iphone, it works perfectly. However if I switch to an Ipad resolution my walls wont move to the edges. Can anybody help me? I cant figure it out, since my walls arent UI elements.

    Sorry for my bad english, and thanks for any answers!

    IPhone_Portrait.PNG IPAD_Portrait.PNG
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    You can get a screen edge (e.g. left) in world space by using:
    Code (CSharp):
    1. var leftScreenEdge = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0, 0, 0)).x;
    And then set the x position of the left wall to leftScreenEdge.
    Here is the right screen edge method:
    Code (CSharp):
    1. var rightScreenEdge = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(1, 0, 0)).x;
    For the z coordinate you would pass in the distance (in meters) between the camera and the 2D content.

    Place the script on the camera.
     
  3. kburkhart84

    kburkhart84

    Joined:
    Apr 28, 2012
    Posts:
    910
    Have you considered that different screen sizes are going to create actual different gameplay? You seem to be changing the actual size of the room/level based on the screen. I don't know your game, but that doesn't seem right, as the game won't be the same for everybody who plays it. It isn't so bad if there is no competition between players, but even so, it won't be the same all over.
     
    User340 likes this.
  4. Zorou_

    Zorou_

    Joined:
    Jan 5, 2020
    Posts:
    7
    Hay,

    thanks for your aswer. Thats a really good point you mentioned there. The main goal is to get as high as possible while avoiding spikes and platforms. Yes, if I just move my walls to the edge of an Ipad screen, the playground is way bigger than on Iphone. But Iam trying to use this variable and somehow make a formula that scales all the other components (spikes etc.) as well. Sure it will never be equal an every device but I can try to minimize the problem
     
  5. kburkhart84

    kburkhart84

    Joined:
    Apr 28, 2012
    Posts:
    910
    You are going about it all wrong. Since it is a camera that is rendering it, the scale won't matter. The objects will stay the same sizes all by themselves. The camera handles the rest automatically. The thing you would need to worry about primarily is the camera show more/less due to aspect ratio of the different resolutions...but there is no need to scale your objects or move them around, rather if you want the view to be the same, you can add black bars to the sides to compensate.

    The only reason this functionality exists for UI stuff is due to the need to attach UI to parts of the screen, like upper left corner, etc... and not have them get distorted. But it doesn't exist for other rendered objects for the reason I mention above.