- Home /
Input.GetTouch(0).position.x and TouchPhase.Began
Hey guys, Is it necessary to insert ..== TouchPhase.Began after Input.GetTouch(0).position.x ? Because they both seem to do the same stuff :
if(touch.position.x < Screen.width/2)
{
//DO STUFF
}
VS
if(touch.position.x < Screen.width/2)
{
if(touch.phase == TouchPhase.Began)
//DO STUFF
}
Comment
Best Answer
Answer by dsada · Jul 13, 2014 at 05:37 PM
Depends on what you would like to achieve. TouchPhase.Began will be true in one single frame if you toch the screen. For example if you would like to make something like that cookie making game and you want something do only once when the player taps the screen you put that in. If you would like a character to follow your finger without constantly need to tap the screen you dont put that in :)
I understand, thanks for the quick reply. So in my case, is it necessary ? (Hold to turn left or right)
void Update ()
{
if(Input.touchCount > 0)
{
int i = 0;
for(i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
if(touch.position.x < Screen.width/2)
{
if(touch.phase != TouchPhase.Ended)
holdingLeft = true;
else
holdingLeft = false;
}
if(touch.position.x > Screen.width/2)
{
if(touch.phase != TouchPhase.Ended)
holdingRight = true;
else
holdingRight = false;
}
}
}
else
{
holdingRight = false;
holdingLeft = false;
}
}