- Home /
Divide screen in 3 equal parts/ Screen.width/3 + use of GetMouseButtonDown(0)
if (Input.GetMouseButtonDown (0) && Input.mousePosition.x < Screen.width / 3) {}
if (Input.GetMouseButtonDown (0) && Input.mousePosition.x == Screen.width / 3) {}
if (Input.GetMouseButtonDown (0) && Input.mousePosition.x > Screen.width / 3) {}
This Line of code beneath divide the screen into 3 equal parts.
Screen.width / 3
I will call them the left, mid and right side. When clicked on the left part of the screen it will trigger the first code line. When clicked on the right part of the screen it will trigger the third code line.
But when you click in between them nothing happends, the second line of code will not be executed. This is because my statement wants you to click exactly in the middle of the screen.
However I don't know how to fix it to make it work when you click in between them. (between the left and right part of the screen, aka the middle part)
I hope you could help me.
-Hexer
Answer by pako · May 23, 2015 at 01:55 PM
Actually it doesn't quite work as you say. You haven't actually defined the middle and right parts correctly. You've defined a left part, which covers pixels 0 to (Screen.width / 3)- 1, a right part which covers almost 2/3 of the screen width, i.e. from pixels (Screen.width / 3) + 1 to Screen.width, and for the middle part you only define it as a single pixel at exactly Screen.width / 3. This is the source of the problem. So, you should redefine the areas as follows:
int desiredWidth = Screen.width / 3;
if (Input.GetMouseButtonDown (0) && Input.mousePosition.x < desiredWidth) {}
if (Input.GetMouseButtonDown (0) && (Input.mousePosition.x >= desiredWidth && Input.mousePosition.x < 2*desiredWidth)) {}
if (Input.GetMouseButtonDown (0) && Input.mousePosition.x >= 2*desiredWidth) {}
Your answer
![](https://koobas.hobune.stream/wayback/20220613200210im_/https://answers.unity.com/themes/thub/images/avi.jpg)