- Home /
For mobile games, how could you divide the screen into three buttons vertically?
Dividing the screen into two inputs is was enough with Screen.width / 2, but now I want to try a game where there are 3 places for input: top, middle, and bottom of the screen, all of equal sizes. I know this can be achieved with buttons, but it seems that using "Input.GetMouseButtonDown" is much faster than what buttons can do. And the accuracy of presses is important in this game for music/rhythm reasons.
You would use Input.Touches and use the Screen.Height to deter$$anonymous$$e if the touch was in one of each 3rd of the screen. Something like,,,
void Update()
{
if (Input.touches.Length > 0)
{
Touch t = Input.touches[0];
if (t.position.y < Screen.height/ 3)
{
//bottom of screen
}
else if (t.position.y < (Screen.height / 3) * 2)
{
//middle of screen
}
else
{
//top of screen
}
}
}
Answer by silvematt · Mar 08, 2019 at 01:33 AM
Hello tablazonsimon
I would go in this way:
public class Controls : MonoBehaviour
{
int splittedScreen = Screen.height / 3;
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
if (Input.mousePosition.y >= 2 * splittedScreen)
{
Debug.Log("Top");
}
if (Input.mousePosition.y >= splittedScreen &&
Input.mousePosition.y < 2 * splittedScreen)
{
Debug.Log("Middle");
}
if (Input.mousePosition.y < splittedScreen)
{
Debug.Log("Down");
}
}
}
}
It is quite simple, you save the value of the Screen Height divided by 3 and with the if statements you check where the mouse is when we tap on the screen.
Hope it helps!