- Home /
The question is answered, right answer was accepted
Unity iOS Swipe not working
Hi! I have this code where if i swipe down i want to activate and deactivate some colliders and the oposite if i swipe up but it does not seem to work.Can someone tell me what is wrong with it?Thanks
void Update()
{
if(Input.touchCount > 0)
{
Touch touch = Input.touches[0];
switch(touch.phase)
{
case TouchPhase.Began:
{
startPos = touch.position;
up.enabled = false;
down.enabled = false;
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 swipe
{
down.enabled = false;
up.enabled = true;
}
else if (swipeValue < 0)//down swipe
{
up.enabled = false;
down.enabled = true;
}
}
break;
}
}
}
}
Could you show us the whole script? Try to insert print statements inside the touchpaseended and check if it is entering there or not. Up could not be referenced somewhere or there might be something wrong with the swipe script.
I know for sure after testing that it reaches the float swipeDistVertical but then it does not go further
Answer by alap soni · Oct 02, 2014 at 10:24 AM
Try using Input.getTouch(0)
instead of input.touches.
Check this code and see if it solves your issue.
void Update() { #if UNITY_ANDROID || UNITY_IPHONE if (Input.touchCount > 0) { Touch touch = Input.touches[0];
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
StartCoroutine(Jump());
break;
case TouchPhase.$$anonymous$$oved:
isSwipe = true;
float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
if (swipeDistHorizontal > $$anonymous$$SwipeDistX)
{
float swipeValue = $$anonymous$$athf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0 && !isTouch)//to right swipe
{
isTouch = true;
StartCoroutine(Right());
}
else if (swipeValue < 0 && !isTouch)//to left swipe
{
isTouch = true;
StartCoroutine(Left());
}
}
//add swipe to up
if(swipeDistVertical > $$anonymous$$SwipeDistY)
{
float swipeValue = $$anonymous$$athf.Sign(touch.position.y - startPos.y);
if(swipeValue > 0 && !isTouch)
{
isTouch = true;
StartCoroutine(Jump2());
}
}
break;
case TouchPhase.Ended:
isSwipe = false;
isTouch = false;
break;
}
}
#endif
} IEnumerator Jump2() { yield return new WaitForSeconds(0.05f); if(playerVelocity <= 0.2f) { Debug.Log("Swipe Up"); } }
IEnumerator Jump() { if (!isSwipe) { yield return new WaitForSeconds(0.05f); if (!isSwipe && playerVelocity <= 0.2f) { Debug.Log("Tap");
}
else
{
yield break;
}
}
else
{
yield break;
}
}
IEnumerator Right() { Debug.Log("Right");
}
IEnumerator Left() { Debug.Log("Left");
}