Some things wrong with swipe move!
float minSwipeDistY; float minSwipeDistX; Vector2 startPos;
if (Input.touchCount > 0){
Touch touch = Input.touches [0];
switch (touch.phase) {
case TouchPhase.Began;
startPos = touch.position;
break;
case TouchPhase.Ended;
float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
if (swipeDistVertical > minSwipeDistY)
{
float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
if (swipeValue > 0 ){ //up
//UP
endPos = new Vector3 (transform.position.x, transform.position.y, transform.position.z + 1);
}else if(swipeValue < 0){ //down
//DOWN
endPos = new Vector3 (transform.position.x, transform.position.y, transform.position.z - 1);
}
}
float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if (swipeDistHorizontal > minSwipeDistX)
{
float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
if( swipeValue > 0){//right
//MoveRight
endPos = new Vector3 (transform.position.x + 1, transform.position.y, transform.position.z);
}else if(swipeValue < 0){//left
//Moveleft
endPos = new Vector3 (transform.position.x - 1, transform.position.y, transform.position.z);
}
}
break;
}
}
Thank you! Could you please teach me how make swipe on touch screen?
Comment