Search Unity

How to get screen point with any scale and size

Discussion in 'UGUI & TextMesh Pro' started by AzeExMachina, Jan 7, 2019.

  1. AzeExMachina

    AzeExMachina

    Joined:
    Jan 30, 2016
    Posts:
    14
    Greetings, I'm trying to get the screen space position of a game object I'm rendering in my UI, the object is not properly on my canvas (which is a Screen Space - Camera) so what I'm doing is

    Code (CSharp):
    1. var point = Viewer.Scene.camera.WorldToScreenPoint(myObject.transform.position);
    2. Vector2 finalPosition;
    3. RectTransformUtility.ScreenPointToLocalPointInRectangle(Viewer.rectTransform, point, Camera.main, out finalPosition);
    I'm calling these lines from a panel inside the canvas, this panel can scale to fullscreen and back, when in fullscreen it has the screen size and localScale = Vector3.one but when it's not it has different sizes, can be any size or scale. My issue is that when it's not fullscreen the variable finalPosition and also the screen position are completely wrong, what am I missing?

    Thanks in advance
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  3. AzeExMachina

    AzeExMachina

    Joined:
    Jan 30, 2016
    Posts:
    14
    @eses
    Sadly this is not what I'm faced with.
    I'll explain a bit more, the point I want to check is not inside my canvas, it's in a world position like (3794.6, 0.0, 3839.1), when I want to see where it is on my canvas with the lines I wrote I get that it's actually in (-40.3, 290.2). I'm then using a secondary camera to get the raw image of this point and placing said raw image on my actual ui canvas. So transform.position of my point would always return me what I wrote before.