rect.contains() not working?
Hello All,
I encountered an error which I wasn't able to solve, and not understand what the problem is. I want to check if my mouse position is over a UI elements rect.transform, but even if it's over, the rect.contains give me false result. The code:
public bool OverGameSpeed(Vector2 coords)
{
return (gamespeedRT.rect.Contains(coords));
}
void Update()
{
Debug.Log("View button: " + viewButton);
Debug.Log("Rect: " + gamespeedRT.position.x + " " + gamespeedRT.position.y);
Debug.Log("RectSize: " + gamespeedRT.sizeDelta.x*gamespeedRT.localScale.x + " " + gamespeedRT.sizeDelta.y*gamespeedRT.localScale.y);
Debug.Log("Mousepos:" + MouseControl_.MouseWorldPoint().x + MouseControl_.MouseWorldPoint().y);
Debug.Log("Contain: " + OverGameSpeed(new Vector2(MouseControl_.MouseWorldPoint().x, MouseControl_.MouseWorldPoint().y)));
OverGameSpeed(new Vector2(MouseControl_.MouseWorldPoint().x, MouseControl_.MouseWorldPoint().y))
}
The mousecontrol converts the mouse position to World coordinates, it's working because if you see the pictures the coordintes should be inside the rect transform. Any advice? What am I missing?
Answer by Esteem · Jan 19, 2020 at 05:35 PM
that's a very convoluted way to check if a mouse is over UI ... Is there any particular reason you're not using the IPointerEnterHandler interface? (I am sorry to be that guy)
Actually I'm checking if the mouse is over another UI element, and not on the object that this script has. Anyway I manage to solve it with Raycast ins$$anonymous$$d, was way more easier.
Answer by lgarczyn · Jan 20, 2020 at 01:37 AM
It looks like your mouse position is (1039:46), which would be above a rectangle of height 215 centered on y 657.
I'm not sure why you're doing any UI work in worldspace though.