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. Dismiss Notice

Positioning with anchoredPosition

Discussion in 'UGUI & TextMesh Pro' started by Gamrek, Nov 30, 2014.

  1. Gamrek

    Gamrek

    Joined:
    Sep 28, 2010
    Posts:
    164
    Hi,

    How do I position a UI object according to an object in world space?

    E.g. Have a UI arrow on top of a character in world space and say click this

    Do I use viewport value? Or Screen point value?

    Setting: Screen Space - Overlay, Reference Resolution: 800, 600
     
  2. expressionpixel

    expressionpixel

    Joined:
    Oct 1, 2012
    Posts:
    20
    This is just a known quick fix because the limitation is that this UI object has to have the anchor preset to the bottom left to match the screen point (0,0 being in the bottom left). I'm sure you can do more math and adjust the x and y if you use another anchor preset other than bottom left. Also I have a feeling that there is some kind of function that will automagically do all this for you no matter what UI settings you are using.
    Code (CSharp):
    1. Vector3 screenPos = Camera.main.WorldToScreenPoint( objectInWorldSpace.transform.position);
    2. m_rectTransform.anchoredPosition = (Vector2)screenPos;
     
  3. Gamrek

    Gamrek

    Joined:
    Sep 28, 2010
    Posts:
    164
    Thanks, I have tried this already. And somehow it is still offset a little bit.

     
  4. expressionpixel

    expressionpixel

    Joined:
    Oct 1, 2012
    Posts:
    20
    So I forgot to mention the parent of this UI object has to also be bottom left, and it has to have a position of 0, 0, with a height and width of 0. If these values are set you have to do more math to adjust this. If you have multiple parents for this object you have to also either set the parent to bottom left, pos 0,0, and height width 0 or keep offsetting it by these values. Another thing that can be effecting this is on your canvas, there is a canvas scaler component, there is a Scale Factor which if its different than 1 you have to to do scale the newpos by this amount.
     
  5. expressionpixel

    expressionpixel

    Joined:
    Oct 1, 2012
    Posts:
    20
    rootCanvasRect is the RectTransform of the root Canvas that has your UI object.

    Code (CSharp):
    1. Vector3 screenPos = Camera.main.WorldToViewportPoint( objectInWorldSpace.transform.position);
    2. screenPos.x *= rootCanvasRect.rect.width;
    3. screenPos.y *= rootCanvasRect.rect.height;
    4. transform.position = screenPos;
     
    Gamrek likes this.
  6. Gamrek

    Gamrek

    Joined:
    Sep 28, 2010
    Posts:
    164
    Thanks,

    I ended up just use an normal sprite and set the sort order very high, it is actually better this way because I would have to position the arrow if the camera moves too. Now I don't have to deal with the camera movement.
     
  7. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    runevision likes this.
  8. Gamrek

    Gamrek

    Joined:
    Sep 28, 2010
    Posts:
    164