RectTransform rect = RCPanel.GetComponent<RectTransform> ();
float width = rect.sizeDelta.x;
float height = rect.sizeDelta.y;
float distanceLeft = Input.mousePosition.x - (RCPanel.transform.position.x - width/2);
float distanceRight = Input.mousePosition.x - (RCPanel.transform.position.x + width/2);
float distanceTop = Input.mousePosition.y - (RCPanel.transform.position.y + height/2);
float distanceBottom = Input.mousePosition.y - (RCPanel.transform.position.y - height/2);
//Debug.Log (Input.mousePosition.x + ", " + Input.mousePosition.y);
Debug.Log (distanceLeft + ", " + distanceRight + ", " + distanceTop + ", " + distanceBottom);
if (distanceLeft < -10) {
RCPanel.SetActive (false);
}
if (distanceRight > 10) {
RCPanel.SetActive (false);
}
if (distanceTop > 10) {
RCPanel.SetActive (false);
}
if (distanceBottom < -10) {
RCPanel.SetActive (false);
}
I am currently using the following script to determine whether the mouse is within 10 units of the mini menu (RCPanel stands for right click panel, i.e. the panel that i want to open when i rightclick). Mathematically it makes sense but there appears to be a flaw in my logic somewhere in the calculation of the 4 distance variables that I believe has something to do with the canvas scaling or conversion between mousePosition in real space and canvas space.
How can I account for the differences so that regardless of screensize or canvas size, the calcuation works correctly. Image provided below of how the mouse doesnt sit where it says it should be sitting. (distance left states a value of 10, but the mouse was positioned just barely to the right of the left border, which should produce a number between 0 and 1 units roughly) (had to redraw the cursor position in to show where it was since it wasnt captured)