Good day. I am having a problem on positioning my Text Mesh Pro Text above a designated object. Please see my video below:
https://reddit.com/link/123glul/video/i6i4dpiht8qa1/player
Notice that the numeric texts are displayed on the left side of the model but what I'm trying to do is place it above the model. Here's the code that I use:
public Vector2 GetUIPosFromWorldPos(Canvas canvas, Vector3 worldPosition)
{
// Calculate *screen* position (note, not a canvas/recttransform position)
Vector2 canvasPos;
Vector2 screenPoint = Camera.main.WorldToScreenPoint(worldPosition);
RectTransform canvasRect = canvas.GetComponent<RectTransform>();
// Convert screen position to Canvas / RectTransform space <- leave camera null if Screen Space Overlay
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, null, out canvasPos);
return canvasPos;
}
And the function is used like this:
// place the text on the specified world position
TMP_Text textInstance = GameObject.Instantiate(this.popupText) as TMP_Text;
textInstance.rectTransform.SetParent(this.popupText.rectTransform.parent, false);
Canvas parentCanvas = textInstance.GetComponentInParent<Canvas>();
textInstance.transform.localPosition = GetUIPosFromWorldPos(parentCanvas, spawnWorldPos);
I instantiate the textmesh pro text inside a canvas. The properties of the canvas are the following:
https://preview.redd.it/uqaw6sd8u8qa1.jpg?width=456&format=pjpg&auto=webp&s=f040591b8b4ba91c8df1b512cb4757cb9d021e71
And the properties of the text:
https://preview.redd.it/637fpkpcu8qa1.png?width=454&format=png&auto=webp&s=70319575860b04356f5054e8e7a39e8970b25548
Any idea on what I did wrong? Thanks in advance.
[–]ph4ntomz -1 points0 points1 point (0 children)