Search Unity

Question Creating a textbubble that follows object from world space, in screen overlay mode

Discussion in 'UGUI & TextMesh Pro' started by DTECTOR, Nov 28, 2022.

  1. DTECTOR

    DTECTOR

    Joined:
    Aug 23, 2020
    Posts:
    132
    Hey guys, I am having a tough time figuring out how to move an element from the ui canvas in screen overlay mode based on the position of an object in the game world space. I've seen a lot of examples but I can't seem to figure it out. Nothing I have done manages to move the position at all.

    This is the line I currently have that is supposed to get the world position and move the textbox/speechbubble to that position. Even if the conversion of the positions is wrong that's fine, but at least I could confirm that I have indeed managed to move the speech bubble. But I can't even get it to move at all.

    Code (CSharp):
    1.  
    2.  
    3. // cameraController.ScreenCam is the reference to the Camera instance in the active scene
    4.  
    5.  
    6.       BanterBubble.GetComponent<RectTransform>().position = cameraController.ScreenCam.WorldToScreenPoint(t.position);
    7.  
    Can someone explain to me what I am doing wrong? Or point me in the right direction to some material that would help me?

    I've tried position & localPosition, at this point I just have no idea. It would seem to me that this would be how to set the position of something and it should reflect it in the game world. Even confirming that this is how to make it move would be helpful as then I would assume something in a parent is anchoring or locking it's position and overriding this line.
     
    Last edited: Nov 29, 2022
  2. DTECTOR

    DTECTOR

    Joined:
    Aug 23, 2020
    Posts:
    132
    So I was able to confirm it's working correctly (or mostly) and that my issue was the camera reference was not being assigned properly in some scenes due to how I was getting the reference. I broke this code a while back making a change and forgetting to test this particular edge case. I figured I'd leave this up in case anyone stumbles upon it. As this code does work and if you see this:

    MAKE SURE TO DEBUG AND CHECK THAT THE OBJECT IS ACTUALLY RETURNING A TRANSFORM AND POSITION!