- Home /
the problem of touch screen logic
en hey.....I got a problem of how to create a button on mobile device, firstly I did a little research about touch screen logic, therefore I got this code:
Rect leftIconRect = new Rect (10, Screen.height-110, 150, 100);
Rect rightIconRect = new Rect(170, Screen.height-110, 150, 100);
Rect JumpIconRect = new Rect (Screen.width-160, Screen.height-110, 150, 100);
void Update () {
foreach(Touch t in Input.touches)
{
Vector2 vec = t.position;
vec.y = Screen.height - vec.y;
if(leftIconRect.Contains(vec)) axisH = -1;
if(rightIconRect.Contains(vec))axisH = 1;
if(JumpIconRect.Contains(vec))jumpB = true;
}
}
It works!!! but when I build a game for android phone, the "jump"touch was not working as I expected. The "touch" is not like button, so if my finger keep pressing on the screen the jump function will always be true!!
so I wonder is there any way to make the "jump" touch as a general button, just working once when I click it!!!!! I'm not sure did I explain the problem clearly..... thank you....
Answer by GameVortex · Feb 04, 2014 at 07:31 AM
Touches in Unity have **touchphases** describing what the touch has just done. All you need to do is check if the touch is in the touchphase.began (this is the equivalent of checking if the mouse has just been pressed down), then set the jump variable to true, if it is not set it to false.
Example:
if(touch.phase == TouchPhase.began)
{
jumpB = true;
}
else
{
jumpB = false;
}
Read the script reference to learn more about how touchphases work.
No problem. =) Remember to mark the question as answered if it solved your issue.