how to set input mouse position, to a rect of a gui button that also needs to follow a different rect

var TargetX = inventoryWindow2.x-45;//inventory rectx
var TargetY = inventoryWindow2.y-55;//inventory recty
GUI.DrawTexture(Rect(TargetX+30,TargetY+30,150,150), inventoryTex, ScaleMode.StretchToFill, true, 0);//inventory picture

        if (GUI.Button(Rect(item1Inv.x + inventoryWindow2.x,item1Inv.y + inventoryWindow2.y, 40,40),GUIContent (texture1)) && item1GUI == 0){
  //the button i want to follow the inventory, but also be where ever i clicked, //item1Inv.x and .y are what are edited and inventorywindow2 is what it follows
        
         		if (example == GameObject.Find("example")) {
        		var u = (Input.mousePosition.x);  
                        var v = (Input.mousePosition.y);
                
        		item1Inv.x = u;//makes it equal to mouse positions,
        		item1Inv.y = v;//
        		}
        
        
        
        //everything works but im not calculating right, ive tried u = mouseposition.x-invetorywindow2.x and many other ways, but none have worked yet to be at the exact mouse position, any ideas would be appreciated!

1 Answer

1

GUI coordinates and screen coordinates are different. GUI coordinates start in the upper left of the screen. Input.mousePosition (screen coordinates) start in the lower left of the screen. Since all your code above has to be in OnGUI(), you should use Event.current.mousePosition inside of OnGUI(). That mouse position is in GUI coordiantes.

Would this still make the position correct? Thank you for your answer very much i will test but im afraid im still not calculating something right -- this was actually how i was getting the position but it still wasnt working which led to how it is now lol but i will still be using even current

it still displays the item far away from the inventory, i need math magic to display it at the point i click, either subtracting the inventory window or dividing somthing, lol i am a formula away

var a : Event = Event.current; var u = a.mousePosition.x - inventoryWindow2.x; var v = a.mousePosition.y - inventoryWindow2.y; never mind- =] thanks very much, now using this^^^^ and the rect itsel being inv + inventory window it shows up Very close to where i click, so thanks again!