Search Unity

Showcase Camera.main.WorldToScreenPoint returning exponentially large negative values as Camera angle changes

Discussion in 'General Graphics' started by cheezy94, Dec 16, 2020.

  1. cheezy94

    cheezy94

    Joined:
    Dec 2, 2017
    Posts:
    3
    Hey everyone,

    I'm wondering why the negative values become exponentially larger and larger as a point in the world goes behind and almost parallel to the camera's side?

    For example, in this simple code, I have a simple Canvas in the World Space and we are tracking it's corners' pixel coordinates through this code:


    Code (CSharp):
    1.         Vector3[] v = new Vector3[4];
    2.         rt.GetWorldCorners(v);
    3.         topLeft = Camera.main.WorldToScreenPoint(v[1]);
    4.         topRight = Camera.main.WorldToScreenPoint(v[2]);
    5.         bottomRight = Camera.main.WorldToScreenPoint(v[3]);
    6.         bottomLeft = Camera.main.WorldToScreenPoint(v[0]);
    7.         Debug.Log(topLeft.x);
    By just observing at the x value of the top left corner of the canvas, when the camera is looking directly at the canvas, the values are normal (they'll range from maybe (-500 to 500 pixels depending on the camera's angle and position). However, as the camera starts rotating away from the top left corner of the plane, the values start becoming -5000, -12,000, -20,000, -50,000, -200,000 and so and so forth UNTIL they become positive all of a sudden out of nowhere. I am guessing that the negative values become so large that it overflows?

    Not quite sure what is going here but would love know how these ScreenPoints become so large and if there is anything that can be down to prevent an overflow while tracking these values accurately.

    Clamping these values with a minimum and max limits did not work correctly due to the fact that the values overflow at some point.
     
    Last edited: Dec 16, 2020