Mouse position

Hi again.

Sorry for making 2 topics in such little time, but I’m experiencing this problem which is annoying me now :frowning:

function OnGUI() {
   if (Input.GetMouseButton(0))
   { 
GUI.Button (Rect (Input.mousePosition.x, Input.mousePosition.y, 60, 60), "click");
  }
  }

Basically I want a gui button to be drawn on the current position of mouse when i click left mouse button.
How ever, when i click, the button appears either much higher or simply not on current mouse position :frowning:
And if i move the mouse while button is pressed, the button will move as well and for example if i move mouse down, button will go up etc.

What am i doing wrong?
Thank’s.

It should be Screen.height - Input.mousePosition.y. The axis is reversed for screen position vs. mouse position.

Edit: Ohh! I also forgot to mention! This will put the upper-left edge of it on the mouse. If you want it to be centered, it should be Rect(Input.mousePosition.x - 30, Screen.height - Input.mousePosition.y - 30, 60, 60).

Ohh, so that’s why!

Thank you, sir.

Runs off to try it out

Yeh, works as a charm :slight_smile:

Hmm… How could I make the button stay on the screen and dont disappear after clicking the mouse?
Now it just stays till the mouse is clicked and when i release it, it disappears :S

Instead of linking the button’s appearance to Input.GetMouseButton, link it to a boolean like “showButton” that becomes true on Input.GetMouseButtonDown(0). So:

var showButton : boolean;
var buttonRect : Rect;

function Update()
{
   if (Input.GetMouseButtonDown(0)) {
      if (!showButton) {
         showButton = true;
         buttonRect = Rect(Input.mousePosition.x - 30, Screen.width - Input.mousePosition.y - 30, 60, 60);
      }
   }
}

function OnGUI()
{
   if (showButton) {
      GUI.Button(buttonRect, "click");
   }
}

Of course, the above will only move it to the mouse and make it appear once. After that, it’s an ordinary non-moving button.

Good idea! How couldn’t I think of that heh.
I’m going to try and make it so the button appears on click and so the boolean is activated, when the mouse moves a little away from the button the boolean deactivates and button disappears.

Who ever is responsible for the forum should implement Reputation points system in here, on the other communities I’m really getting used to giving people who help me some reputation (or Karma) and it just seems a little weird when I can’t give out “rep” in here ;D

Sorry to chime in here

But is there a way to stop the button following the mouse? Appear where the mouse clicked and stay there.

Thanks