Problem with mouse position

Hi,

I want to do tooltips wich follows the mouse.

My problem is that the Gui isn’t at the right place and when I start from the top left corner my mouse is on the GUI and when i’m in the bottom right corner the gui is still in the middle of the screen.

I’m sure it’s just a matter of finding the good equation and I’ll probably it find during my sleep but if someone has the answer now that could be usefull !

here is my script to have a moving GUI.

 MX = Input.mousePosition.x;
        MY = Screen.height - Input.mousePosition.y;
        //print(MX);
        print(MY);

        TTpx = MX;
        TTpy = MY;
........
            else if (TTnumber == 14)
            {
                ToolTip = TT_Mast;
                TTx = 88;
            }
            else if (TTnumber == 15)
            {
                ToolTip = TT_Rudders;
                TTx = 125;
            }
            else if (TTnumber == 16)
            {
                ToolTip = TT_Trampoline;
                TTx = 155;
            }
            GUI.Label(new Rect(TTpx, TTpy, TTx, TTy), "", ToolTip);

Here few screenshots of the problem (the red dot is the mouseposition in reality).

Thank"s a lot for the help

BooBi



For one thing, use Event.current.mousePosition in OnGUI functions, not Input; then you don’t have to mess with coordinates. I guess “TTx” is the tool tip X position, which you shouldn’t be hard-coding; use the mousePosition instead. Also you’re a lot better off using arrays instead of a zillion “else if” statements.

–Eric

Hi,

thank’s it’s working fine!

I know that I should optimize a lot my script by using arrays, can I ask you some explainations about that, I was using a lot them in quest (longtime ago) but since I’m in unity, I don’t know why I stopped using them and I know I’m loosing a lot of time and performances with all these if…

thank’s a lot for your help