- Home /
Why doesn't Unity like my rectangles?
So I'm having some trouble with one of my rectangles. Here are the rectangles I'm using:
var leftHandRect = Rect.MinMaxRect(0.22*Screen.width,(2/3)*Screen.height,0.4*Screen.width,Screen.height);
var rightHandRect = Rect.MinMaxRect((2/3)*Screen.width,(2/3)*Screen.height,0.83*Screen.width-1,Screen.height);
var bothHandRect = Rect.MinMaxRect(0.4*Screen.width,(2/3)*Screen.height,(2/3)*Screen.width,Screen.height);
The code works fine for leftHandRect, I haven't added functionality to bothHandRect yet so I can't test that, but it doesn't like rightHandRect at all. When I first tried it out, I had slightly different rectangles, and the left one was working fine, but the right one did nothing. (When I say working, I mean using leftHandRect.Contains to check a touch event and then do something. I know the 'do something' code works, cos it was working earlier and works with the left hand rect, but now that the right hand rect is the way it is, it takes over and picks up every touch event no matter where it is on the screen, and the bounds of the rect are clearly not the whole screen. Any ideas? Cheers
Ok so I half figured it out. Realised that my approximated maths probably meant that the rectangles were going below the bottom of the screen. Fixed that, and well only one of the rectangles got fixed. Still got a problem with one. There must be a better solution.
the above comment makes no sense now that I've edited the question. I've been having quite a bit of trouble with these rectangles!
Answer by lukewilliams · Feb 24, 2014 at 02:03 PM
Ok so I figured it out.
2 things to note:
Unity doesn't like fractions of Screen.width and Screen.height. Perhaps a bug, or not rounding pixels correctly.
And secondly, there's an error in the reference for Rect. At least for Rect.MinMaxRect, xMin and yMin are actually the bottom left hand corner, not the top. Hope this helps some people! And hopefully someone from Unity will see this and update the reference! (I don't know how to contact them directly).
Unity likes fractions of Screen.width and Screen.height of course. You can't place anything if not.
hmm well I tried using fractions of Screen.width and Screen.height and it didn't work for me, but as soon as i changed stuff like (2/3) to 0.66 it worked. $$anonymous$$aybe its a bit buggy. Who knows. Glad it works for you, it sure annoyed me for a long time.
i think that is because you would need to say 2f/3f to get a fraction else int
Your answer
![](https://koobas.hobune.stream/wayback/20220613134546im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What could be causing this problem reading from a Rect into a Vector2? 3 Answers
Debug.draw rect 2 Answers
Rect.Contains doesn't appear to be working with Input.GetTouch.position 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers