- Home /
Divide screen into two touch buttons
Hey!
I'm trying to make a object rotate based on if the user touches the left side of the screen or the right side of the screen. If the user presses the left side of the screen before releasing the right side of the screen (or the other way around), the new input should be the one that object rotates after. And when the user is not touching the screen the rotation should be zero.
I have currently come up to this code which doesn't work at all.
void Update()
{
int i = 0;
while (i < Input.touchCount) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
if(Input.GetTouch(i).position.x < 0)
playerMovementDirectionFromInput = -1;
if(Input.GetTouch(i).position.x > 0)
playerMovementDirectionFromInput = 1;
}
++i;
}
playerMovementDirectionFromInput = 0;
}
and the player is rotating by a transform.Rotate code where playerMovementDirectionFromInput is multiplied to.
A tried using the void OnMouseDown() with two colliders which works but this method falls when the user is pressing the other button before releasing the first one. So i think the only way of doing this is with Input.GetTouch.
What is wrong?
(I'm new into using touch inputs so feel free to treat me like a fool)
got this code working
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch (0);
if (touch.position.x < Screen.width/2)
{
player$$anonymous$$ovementDirectionFromInput = -1;
}
else if (touch.position.x > Screen.width/2)
{
player$$anonymous$$ovementDirectionFromInput = 1;
}
}
else
player$$anonymous$$ovementDirectionFromInput = 0
Answer by SunnyChow · Feb 03, 2016 at 04:33 AM
I think you already found it out. (Input.GetTouch(i).phase == TouchPhase.Began) only trigger the below functions only in the beginning of a pressing.
There is a drawback of directly using Input for game control. If you creates some 2d buttons (eq: pause button). It still trigger the player movement even the user is pressing the ui buttons. It will be easier to manage if it's based on gui
Your answer
![](https://koobas.hobune.stream/wayback/20220613122022im_/https://answers.unity.com/themes/thub/images/avi.jpg)