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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

WorldToScreenPoint doesn't work on Y screen axis

Discussion in 'Scripting' started by SimonAlkemade, Nov 10, 2009.

  1. SimonAlkemade

    SimonAlkemade

    Joined:
    Feb 4, 2009
    Posts:
    432
    Hi, I am trying to attach a simple screen box with some text to a 3D object. For the X axis this works correctly but on the Y axis the transform seams to be off a little bit. Furthermore when I translate the object or the camera the drawn box translates in the opposite direction of which it should translate.

    Code (csharp):
    1.  
    2. var posX = 0;
    3. var posY = 0;
    4.  
    5. function OnGUI () {
    6.     GUI.Box(Rect(posX,posY,100,20),"some info");
    7. }
    8.  
    9. function Update () {
    10.     var screenPos = Camera.main.WorldToScreenPoint (transform.position);
    11.  
    12.     posX = screenPos.x;
    13.     posY = screenPos.y;
    14. }
    15.  
    Does anyone else experience the same problem?
    What am I doing wrong?
    Why does it work on X but not on Y?

    Help is very much appreciated.

    Cheers,
    Simon
     
  2. DMJ

    DMJ

    Joined:
    Nov 5, 2009
    Posts:
    83
    "Seems off a little bit"? How so? And how much? Is it by a consistent amount?

    Are you sure your axes are set up the way you expect, with positive directions in the direction you expect?
     
  3. SimonAlkemade

    SimonAlkemade

    Joined:
    Feb 4, 2009
    Posts:
    432
    well after some experimenting with Debug.Log() it appears that WorldToScreenPoint() returns a lot more then just a X and Y coordinate. It seams to return 6 values of which the first value is the X coord. the second looks like a static value and the third value appears to be changing the most when I translate the object or the camera on the Y axis. However to yield this number I tried to use posY = screenPos.z; which gives a closer result but is still off by and offset of about 200 pixels. Also it doesn't allign perfectly. Note z here should be the distance to the camera and not the height in pixels which I am looking for.
    Accoording to the documenation WorldToScreenPoint() should return a Vector 3 were X and Y are width and height and Z is depth or distance from the point to the camera in world space units.

    Anyway I find it quite weird, first I am gonna try a reboot of my system maybe that some pointer somewere is trying to acceess an invalid memory location or something like that. First I'll do a restart and if the problem persists I will post here again.
     
  4. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    The y axis is flipped on WorldToScreenPoint.

    HEre is some code for putting a label on a target object...

    (Making code...)
     
    glenneroo likes this.
  5. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    Here it is!
     

    Attached Files:

    trul and Graph like this.
  6. SimonAlkemade

    SimonAlkemade

    Joined:
    Feb 4, 2009
    Posts:
    432
    Thank you Flynn.
    This solves my problem with a simple Screen.height - screenPos.y;

    maybe the documentation should say that the Y axis is flipped and this can be corrected by using the screen height minus the output of the y component.

    Anyway thanks a lot
     
    glenneroo likes this.
  7. leenephi

    leenephi

    Joined:
    Apr 11, 2012
    Posts:
    2
    Wow. Thank goodness.. that simple Y issue was my problem!
     
    trul likes this.
  8. RemusRain

    RemusRain

    Joined:
    Mar 9, 2019
    Posts:
    15
    For anyone wondering what the solution was without downloading the package:

    Code (CSharp):
    1. Vector2 position = Camera.main.WorldToScreenPoint(transform.position);
    2. position.y = Screen.height - position.y;
     
    ScIobazs, gooby429 and fmasutti like this.