3

I try to figure out if a mouse click was inside of a rect transform or not.

Each time the user clicked on the screen, a ball will be thrown. But in case the user clicks on the pause button, no ball should be thrown.

I tried to solve with this piece of code but it seems that only the top right quarter of the rect transform is recognized. Here's a short video to show the actual problem: https://youtu.be/gdyDBK6ubgo

Here's the code snippet:

void Update() {
     //Check if user touch on display / click mouse button 
     Vector2 mousePos = new Vector3(Screen.width - Input.mousePosition.x,Screen.height - Input.mousePosition.y, 0); 
     if (Input.GetMouseButtonDown(0) && props.throwable && !checkCollisionWithPauseButton(mousePos) && props.remainingBalls > 0)
     {
         fireBall(Input.mousePosition);
     }
 }

 bool checkCollisionWithPauseButton(Vector3 mousePos){
     //TODO: This does not work very well
     return pauseButton.GetComponent<RectTransform>().rect.Contains (mousePos); 
 }

Here's a screenshot which shows the rect transform.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
Marvin Krüger
  • 127
  • 1
  • 1
  • 11
  • why dont you just use the event system like its supposed to work? https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html – yes Jan 28 '18 at 21:27
  • Thanks a lot - I wasn't aware of this method! But any idea why this dosen't works like expected? – Marvin Krüger Jan 28 '18 at 21:55
  • Without much thinking about it, I think its related to that `Input.mousePosition` already is in screen space. Try to output all your calculations and values you compair against with `Debug.Log(string)` to console and look what you get. You probably will be able to draw conclusions on your own. – yes Jan 28 '18 at 21:59
  • Please don't post links to image hosting webpages which force to deactivate add blockers... Instead upload the image directly here to your question. – derHugo Jan 29 '18 at 21:33

3 Answers3

7

Use RectTransformUtility.RectangleContainsScreenPoint to check if the mouse pointer is in the Rect Transform. This will work regardless of where the rectTransform is positioned locally.

public RectTransform rectTransform;

...

Vector2 mousePos = Input.mousePosition;
RectTransformUtility.RectangleContainsScreenPoint(rectTransform, mousePos);
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
3

None of these worked in my case. Here is what I ended up doing:

public RectTransform rectTransform;

...

Vector2 lp;
Vector2 mousePos = Input.mousePosition;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, mousePos, Camera.main, out lp);

if (rectTransform.rect.Contains(lp))
    ..ect
Zock77
  • 951
  • 8
  • 26
0

It's not working because you're performing some unnecessary work. You don't need this line:

Vector2 mousePos = new Vector3(Screen.width - Input.mousePosition.x,Screen.height - Input.mousePosition.y, 0); 

Simply pass Input.mousePosition to the Rect.Contains function and that should work.

Vector2 mousePos = Input.mousePosition;
pauseButton.GetComponent<RectTransform>().rect.Contains (mousePos);

Even though that may work, this is not how to detect clicks properly on UI or GameObjects. The EventSystems is used for that. See this for example.

Programmer
  • 121,791
  • 22
  • 236
  • 328