- Home /
Right click, gui.box appears at mouse position, freezed?
I have this code. I'm trying to make it like runescape, you right click, and a drop down menu appears, with option for where you rightclicked. It works! All I need it to do, is not follow the mouse cursor after it's been right clicked! Any ideas?
#pragma strict
var hit : RaycastHit;
var RightClickTrue = false;
var shopOpen : boolean = false;
//
var boxOpen: boolean = false;
var boxPos: Vector3;
//
function OnGUI () {
if(shopOpen == true) {
GUI.Box(ResizeGUI(Rect(350,350,50,50)), "General Shop");
}
if(RightClickTrue == true) {
}
if (boxOpen == true) {
GUI.Box (Rect (boxPos.x,boxPos.y,120,120), "");
}
if (RightClickTrue == true) {
boxPos.x = Input.mousePosition.x;
boxPos.y = Screen.height - Input.mousePosition.y;
boxOpen = true;
}
}
function Update () {
if(Input.GetMouseButtonDown(1) &&
collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit,
Mathf.Infinity)) {
RightClickTrue = true;
}
}
function ResizeGUI(_rect : Rect) : Rect {
var FilScreenWidth = _rect.width / 800;
var rectWidth = FilScreenWidth * Screen.width;
var FilScreenHeight = _rect.height / 600;
var rectHeight = FilScreenHeight * Screen.height;
var rectX = (_rect.x / 800) * Screen.width;
var rectY = (_rect.y / 600) * Screen.height;
return Rect(rectX,rectY,rectWidth,rectHeight);
}
Answer by koray1396 · Apr 18, 2014 at 08:58 PM
Use another variable;
if(!RightClickTrue){
mousePos = Input.mousePosition;
} else {
boxPos = mousePos;
}
of course you have to declare it first, above was intended for a general idea of how it would function.
Actually, looking at your script I realized there is no need for another variable, you already have more than needed.
so it should be
var boxPos : Vector2; // no need for Vector3, you only use x and y
then under Update(), you should be storing the mouse position with the click.
if(Input.Get$$anonymous$$ouseButtonDown(1) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit,$$anonymous$$athf.Infinity)) {
RightClickTrue = true;
boxPos.x = Input.mousePosition.x;
boxPos.y = Screen.height - Input.mousePosition.y;
}
then under OnGUI();
if(RightClickTrue == true) {
GUI.Box (Rect (boxPos.x,boxPos.y,120,120), "")
}
you can also remove boxOpen, since RightClickTrue will be true only when the box opens.
hope this helps.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143037im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Detect Right Click on GUI Buttons 2 Answers
Inventory GUI 0 Answers
How will i develop the type of gui? 0 Answers
OnGui Function help 1 Answer