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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Do objects adhere to screen's boundaries at any screen size

Discussion in '2D' started by dnlcl, Oct 11, 2020.

  1. dnlcl

    dnlcl

    Joined:
    Aug 12, 2020
    Posts:
    9
    Hi, it seems to be an easy problem but I can not figure out how to achieve this.
    I need to place a sprite on the top, left and right edge of the screen, as I did like this and so far so good:



    But the problem is that if I change the resolution from 1920x1080 to 1792x828 (or other) than I got this:



    I would that the sprites automatically adhere to the screen's edge at any screen size. How could I accomplish that? Can you please give some hint?
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Well, UI elements can do this quite easily with anchoring, but since these appear to be game objects that's rather unsuitable to use the UI. You can, although I do question why you'd want you game field to alter its effective characteristics by resolution. That is generally a nono. Or do you plan to have your game behavior adjust as well to ensure the same behavior?

    Now for game objects you can get the screen resolution and adjust their coordinates accordingly in the start function. To get the screen resolution you have to use Screen.height and Screen.width if you're in windowed mode, and Screen.currentResolution if you're in full screen mode. If you are planning to have behavior adjust, any moving object will likewise need to be scale and move at the appropriate ratio for the resolution in both dimensions.

    I'm not familiar with phone based programming myself, although I'd suspect most games just set their resolution and let the phone handle scaling as appropriate, but I could be wildly wrong there. Just remember the editor ignores any resolution alteration calls so if you want to test those out you have to actually make a full build.
     
    dnlcl likes this.
  3. ali_unity529

    ali_unity529

    Joined:
    Feb 26, 2020
    Posts:
    1
    If we attach script to objects to stay at boundry using ViewportToWorldPoint the objects stay at the boundry. checke here.