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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UI Objects in World Space Constant Size

Discussion in 'General Graphics' started by Talthilas, Jun 1, 2015.

  1. Talthilas

    Talthilas

    Joined:
    Apr 1, 2014
    Posts:
    44
    Hi all,

    I would like to place some labels and sprites (think Civ5 city names) at certain locations in world space. I would like the objects to remain a constant size no matter where i move or zoom the camera. Basically exactly the same functionality as the icon tags you can give objects in the Unity editor. You will notice they remain the same size no matter what you do with the camera.

    How do you achieve this?

    Thanks.
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    I recommend having an empty game object in world space where you want the text, then calling camera.WorldToScreenPoint() sending in the position of the empty gameobject. That will give you a position on your screen, then all you have to do is put text in that position screen-space.
     
  3. IARI

    IARI

    Joined:
    May 8, 2014
    Posts:
    70
    Same Problem: I want Info displayed within a Canvas for certain World elements.
    I want the Canvas hovering over or next to their world elements.
    The Canvas should have a constant size with respect to the Screen.

    BUT I want occlusion for the Canvas:
    The Canvas should have an actual world position, and If a Part of the canvas is covered, it should preferably be rendered semi transparent (or not at all).

    So in that case WorldToScreenPoint does not seem like a good option. Any suggestions?
     
    Walter_Hulsebos likes this.
  4. DemeDev

    DemeDev

    Joined:
    Oct 18, 2017
    Posts:
    7
    Ok I found a way to fix it.
    The canvas is in world space render mode.

    For the rotation you just put canvas.transform.rotation = camera.transform.rotation;

    And for the constant size, relative to the distance:
    dist = Vector3.Distance( canvas.transform.position, camera.transform.position);
    canvas.transform.localScale = Vector3.one * dist / initial dist;

    the initial dist is the distance you used to draw the UI element. For example I drew an hp bar from 5 meters, so initial dist would be 5.
    To calculate this distance you can put the canvas at the position (0,0,5) 5 for Z axis, and then put your camera at (0,0,0).
    So you are seeing the UI element from 5 meters (in the game tab) and you set the height and width from that distance. So if you want to keep that size constant.
    localScale = Vector3.one (1,1,1) * dist/ 5;
    dist / 5 = 1 at start, so you keep local scale (1,1,1) at the beginning as you drew it.

    Sorry for my bad english lol
     
  5. Fressbrett

    Fressbrett

    Joined:
    Apr 11, 2018
    Posts:
    83