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

ScreenToWorldPoint always the same

Discussion in 'Editor & General Support' started by projectmwgames, Jun 29, 2015.

  1. projectmwgames

    projectmwgames

    Joined:
    Nov 17, 2013
    Posts:
    9
    I'm trying to get the world point of the screen height. this is the code:

    voidUpdate () {
    Vector3 fullScreen = Camera.main.ScreenToWorldPoint(newVector3(Screen.width,Screen.height,10));
    print ("screen.height "+Screen.height);
    print ("fullScreen.y " + fullScreen.y);
    }

    I then test with iphone 4 dimensions (320x480). I get:
    screen.height 480
    fullScreen.y 5

    I shrink the window and screen.height updates but fullScreen.y is still 5. I retest on iphone 5 dimensions (320x568). I get:
    screen.height 568
    fullScreen.y 5

    why is it always 5 when Screen.height changes?
     
  2. Giometric

    Giometric

    Joined:
    Dec 20, 2011
    Posts:
    170
    Unity uses a vertical FOV, so even if your height changes, since you're always passing the screen width and height (top-right corner of the screen), the world space y coordinate would always be the same, unless your camera was rotated. In this case, your aspect is changing, so the x coordinate should be changing.
     
  3. projectmwgames

    projectmwgames

    Joined:
    Nov 17, 2013
    Posts:
    9
    yes, the x / width was behaving as expected which confused the heck out of me. so, how do I do what I want to do?


    The reason I want the world screen height is because for a mobile game, my main game area is scaled full width and then the height is to a certain ratio. I'm trying to scale the top and bottom blocks to fill the remaining area. The scale amount would be something like (fullScreen.y- main.height)/2 but I can't get the world screen height. The other option is to convert to screen space but I can't figure out how to convert main.height into screen space either (can only convert points?)
     
  4. projectmwgames

    projectmwgames

    Joined:
    Nov 17, 2013
    Posts:
    9
    hm... gonna try and convert the main.height using pixel to units ratio tonight and see how that goes..
     
  5. Giometric

    Giometric

    Joined:
    Dec 20, 2011
    Posts:
    170
    What are the grey blocks, exactly? Are you trying to create the 'black borders' thing around the top and bottom of your screen?

    If that's the case, you could use some black image objects in a 2D Canvas UI, anchored to the top and bottom. You would just need to determine their correct sizes based on what the screen resolution aspect is. And again, since the FOV in Unity is vertical, you will probably want to modify the Rect on your main camera to shrink the vertical size a bit, otherwise it will draw the top and bottom underneath the black bars. The camera Rect is given in normalized viewport coordinates (0 to 1), but it should be possible to create the right values without needing to know the actual pixel sizes. You only need to know what Screen.width and Screen.height are so you can check what the aspect is.

    For example, 320x480's aspect is 0.6667; this is your desired aspect. Let's say someone is on iPhone 5s, so their actual res is 640x1136. The aspect of this is 0.5634, a lower number (meaning a larger height, relatively) than your desired aspect. If you multiply your actual width by the aspect you want (640 * 0.6667), you'll get what the height should be at the desired aspect, which is 960. 960 divided by 1136 is 0.8451. This the height in viewport coordinates that your camera should render to. The y coordinate (offset from the bottom) will be the (1.0 - viewport size) / 2.0. So (1.0 - 0.8451) / 2.0 = 0.07745. The x and width are both their defaults, 0 and 1 respectively, since you want to take up the entire width.

    This math is off the top of my head, but I think it should work. In the case that a given resolution is wider than your target aspect, you do the same math, but with the width instead.
     
  6. projectmwgames

    projectmwgames

    Joined:
    Nov 17, 2013
    Posts:
    9
    yes, that's what I'm trying to achieve. That sounds like it could work. I'll report back later.

    thanks for the idea Giometric
     
  7. projectmwgames

    projectmwgames

    Joined:
    Nov 17, 2013
    Posts:
    9
    it worked. you're a legend. here's the code for anyone else:

    floatmainRatio = 4f/3f;
    floatscreenMainRatioHeight = Screen.width * mainRatio;
    floatscreenMainRatio = screenMainRatioHeight / (float)Screen.height;
    floatyScale = (1 - screenMainRatio) / 2f;

    //fullscreen
    gameObject.transform.localScale = newVector2 (1,(float)Screen.height/(float)Screen.width);

    gameObject.transform.localScale = newVector2 (1,yScale*gameObject.transform.localScale.y);

    I put this on a ui image that's just a white square of 320x320px.

    thanks again Giometric :D