Search code examples
c#unity-game-enginetextclickuiappearance

UNITY Appear text next to mouse click


I want to make a text that appears next to the mouse button, where i click on a button. So i have a huge button, and i want that text to appear next to the mouse/touch screen where i clicked. How can i do it?

I'am new in this section so please write it down easily for me! And if someone could help with the animation of this text as well, to pop up then move up and dissappear just when i click!

Something is bad with my text so the site don't let me post it, so i'll try to put something else here, hopefully it'll let me post it finally.! Thanks a lot!


Solution

  • You should use Input.mousePosition. As the documentation says, the origin (0,0) is on bottom left corner and for get topright corner you should use (Screen.width, Screen.height).

    For 2d Got mouse position with:

    Vector3 mousePos = Input.mousePosition;
    

    For 3dWorld use:

    Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition)
    

    Apply this position to your text object adding some offset:

    myText.transform.position = mousePos + offSetVector;