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

Get GameObject's position in global screen space. How?

Discussion in 'Getting Started' started by Sparticus, Sep 8, 2022.

  1. Sparticus

    Sparticus

    Joined:
    Mar 15, 2014
    Posts:
    149
    Quick question...

    I have a User Interface I am working on. I have my Canvas set to "Screen Space - Overlay"

    Within that UI, I have a GameObject nested many levels deep inside other game objects. I want to find the Vector3 transform (it's location) of that specific object in Global Screen Space.

    Currency if I try and print out the objects position :

    Debug.Log(myObject.gameObject.transform.position)


    I am getting :
    (12.3, 254.0, 0.0)


    Yet this GameObject is roughly in the center of the screen. I am assuming that's it's position relative to it's parent.

    How can I get it's position relative to the canvas screen (which is set to 1920x1080)

    I'm hoping to get a position of something like
    (950, 500, 0.0)


    Thanks!!
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,505
  3. Sparticus

    Sparticus

    Joined:
    Mar 15, 2014
    Posts:
    149
    Ya, I tried that before and it doesn't appear to work as expected :

    this :

    Debug.Log(this.timeLeftTitle.gameObject.transform.position);

    prints out
    (12.3, 254.0, 0.0)


    And this :

    Debug.Log(Camera.main.WorldToScreenPoint(this.timeLeftTitle.gameObject.transform.position));

    prints out :
    (0.0, 0.0, 0.0)


    What am I missing?
     
  4. Sparticus

    Sparticus

    Joined:
    Mar 15, 2014
    Posts:
    149
    I feel like 'WorldToScreenPoint' or anything like that is the wrong method. At the end of the day, I just want to convert local screen space to global screen space. I'm struggling to locate the answer on google... hoping someone knows the answer :)
     
  5. Sparticus

    Sparticus

    Joined:
    Mar 15, 2014
    Posts:
    149
    Worst case scenario I loop through a game objects parents (recursively to the root) and calculate where it is in the global screen space... but there must be a method already provided to do this. I can't seem to find the answer anywhere :(
     
  6. BobitaSugar

    BobitaSugar

    Joined:
    Sep 11, 2022
    Posts:
    1
    I don't think it's possible. Sorry
     
  7. Mminder360

    Mminder360

    Joined:
    Nov 13, 2015
    Posts:
    1
    this is what I did and it seems to work

    public Vector3 GameObjectPosition;
    private void FixedUpdate()
    {
    GameObjectPosition = Camera.main.WorldToScreenPoint(transform.position);
    }


    hope this helps
     
    Last edited: Nov 23, 2022
  8. Mibutec

    Mibutec

    Joined:
    Jul 30, 2021
    Posts:
    6
    Bro, I feel you. Could you finish your worst-case scenario method?
    I'm also that far to write a recursive method to find position, but am really afraid of all the unknwos lurking doing that.
     
  9. Mibutec

    Mibutec

    Joined:
    Jul 30, 2021
    Posts:
    6
    Btw. in most cases

    Camera.main.WorldToScreenPoint(transform.position);


    is fine. However, in some constellations of pivots and/or anchors it seems to produce crap, just like

    transform.InverseTransformPoint(origin.position)