Home > OS >  Make a text stick to some coordinates in Unity
Make a text stick to some coordinates in Unity

Time:03-16

Hi everyone I have a text in my UI and I don't want it to follow the movements of the player camera but I want to make it still in the point I instantiated it.enter image description here

The text is the little yellow phrase you see under the "move" ui buttons on the left. When I move my character it follows the camera. I want it to stay where it is.

This is the code I use to instantiate it:

void SpawnPopupText()
    {
        TextMeshProUGUI popupInstantiated = Instantiate(startLevelMessage, pointWhereToSpawnMessage.transform.position, pointWhereToSpawnMessage.transform.rotation);
        popupInstantiated.transform.SetParent(canvas.transform, false);

        Vector2 screenPosition = Camera.main.WorldToScreenPoint(pointWhereToSpawnMessage.transform.position);
        popupInstantiated.enableWordWrapping = false;
        popupInstantiated.text = messageToStartLevel;
        popupInstantiated.color = colorOfTextPopup;
        popupInstantiated.transform.localScale = dimensionsOfPopup;
        popupInstantiated.transform.position = screenPosition;
    }

Is there any way to do this clean? Thank you in advance. Have a nice day.

-NV

CodePudding user response:

I would use world space.

There's a nice 7.5 min tutorial of how to place UI elements in the world (instead of on screenspace) here: https://www.youtube.com/watch?v=Zwgj3mwOVlg&ab_channel=CodeMonkey

Here's another nice one: https://www.youtube.com/watch?v=GuWEXBeHEy8&ab_channel=aboutgamemaking

In essence you place your UI-elements in "world space" image of the canvas in world space.

  • Related