- Home /
Question by
suarvae · Mar 15 at 04:04 PM ·
swipeinput.touch
How to do half Screen Swipe Controls
Hello, I'm trying to use swipes performed on one half of the screen to control my player. The swipes registered are consistent, sometimes detecting the wrong direction even. My update function looks like this
foreach (Touch touch in Input.touches)
{
if (touch.position.x < Screen.width / 2)
{
if(Player.transform.position.z == 0) // initialize the players run cycle
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
Player.direction.z = forwardspeed;
}
}
else
{
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
startTouchPos = Input.GetTouch(0).position;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
currentTouchPos = Input.GetTouch(0).position;
Vector2 distance = currentTouchPos - startTouchPos;
if (!stopTouch)
{
if(distance.x <-swipeRange)
{
swipeText.text = "Left";
stopTouch = true;
}
else if (Distance.x > -swipeRange)
{
swipeText.text = "Right";
stopTouch = true;
}
else if(Distance.y > swipeRange)
{
if(Player.controller.isGrounded)
Player.Jump();
swipeText.text = "Up";
stopTouch = true;
}
}
}
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
endTouchPos = Input.GetTouch(0).position;
stopTouch = false;
}
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613054221im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to detect a mobile double tap, and a long press. PS. I already have swipe detected. 0 Answers
How to touch anywhere on screen? 0 Answers
Swipe control for circle (Can do swipe left/right) 2 Answers
iPhone - How to calculate a decent touch swipe speed? 2 Answers
Recommendations on best way to begin implementing Swipe for iOS game? 1 Answer