- Home /
Contains(Input.mousePosition) reacts to wrong rect
In my code I have 12 buttons. All with their own functionality and all work as intended. For six of them I added a "MouseOver" functionality and I now want to do that for the other six too.
But I am running in a weird problem.
Definition of the buttons:
private Rect ButtonO1;
private Rect ButtonO2;
private Rect ButtonO3;
private Rect ButtonU1;
private Rect ButtonU2;
private Rect ButtonU3;
private Rect ButtonLO1;
private Rect ButtonRO1;
void Start () {
ButtonO1 = new Rect(Screen.width/2+70,Screen.height/5-55,50,50);
ButtonO2 = new Rect(Screen.width/2-25,Screen.height/5-55,50,50);
ButtonO3 = new Rect(Screen.width/2-120,Screen.height/5-55,50,50);
ButtonU1 = new Rect(Screen.width/2+70,Screen.height-90,50,50);
ButtonU2 = new Rect(Screen.width/2-25,Screen.height-90,50,50);
ButtonU3 = new Rect(Screen.width/2-120,Screen.height-90,50,50);
ButtonLO1 = new Rect(Screen.width/2-200,Screen.height/5+25,50,50);
ButtonRO1 = new Rect(Screen.width/2+150,Screen.height/5+25,50,50);
}
In the Update function I have this
void Update () {
if (ButtonO1.Contains(Input.mousePosition) || ButtonU1.Contains(Input.mousePosition)) {
OnMouseOverO1();}
else if (ButtonO2.Contains(Input.mousePosition) || ButtonU2.Contains(Input.mousePosition)) {
OnMouseOverO2();}
else if (ButtonO3.Contains(Input.mousePosition) || ButtonU3.Contains(Input.mousePosition)) {
OnMouseOverO3();}
else if (ButtonLO1.Contains(Input.mousePosition) || ButtonRO1.Contains(Input.mousePosition)) {
Debug.Log("Button L1 oder R1");}
else {
OnMouseExit();}
}
The first six buttons work flawlessly. When I hover over them it does what it is supposed to do. If I hover over the ButtonLO1 or RO1 nothing happens. The Debug.Log command is only processed when I hover over two of the remaining four buttons. And I have no idea why!!! If I press the ButtonLO1 it works. So some part of the engine realizes I am hovering and clicking that button even so the Contains(Input.mousePosition) clearly doesn't.
Does anyone know why the engine confuses those buttons? Why does it nothing happen if I hover over ButtonLO1? Should I report a bug?
I recall having to invert the screen 'y' coordinate at some point. You may need to add 'Screen.height-' in front of your 'y' components
Answer by Kashrlyyk · Jun 22, 2012 at 10:08 AM
Thanks, that was exactly what I wanted to test today.
Is there any reason why the "Input.mousePosition" starts in the lower left corner and the position of "Rect" starts in the upper left corner? Why are they different?
I guess it's related to the functions Camera.ScreenToWorldPoint and co.
I have asked this question for the past twenty years. No two coordinate systems are the same and I always bash my head against a wall for a few $$anonymous$$utes before I remember mouse is down and screen is up or Y is up here and Z is up there (and I think X was up once...)