Limiting actions when using TouchPhase.Moved \\ Swipe Control Help
Hi All.
I'm currently making an endless runner in Unity. Three lanes, swipe to move into different lanes that are in set increments along the x-axis . I've got the swipe detection down, I think. My problem is with responsiveness. I tried using TouchPhase.Ended when calculating the swipe direction etc. but that means that the player only moves once the finger is lifted, which feels unresponsive.
I then tried using TouchPhase.Moved but found that because it's updating as the finger moves, the player movement is function is called too often, if that makes sense, and throwing the player object off of the set increments.
Is there a way to get the player object to start moving while the swipe is still happening, but only move again once a separate swipe is registered? In other words I want to only have one movement per swipe but I want the movement to happen while the swipe is happening. Is that possible?
Please have a look at the code I'm currently using. I am quite a noob. Please can someone point me in the right direction?
Thank you.
private Vector2 fingerDown;
private Vector2 fingerUp;
public bool detectSwipeAfterRelease = false;
public float swipeThreshold = 20f;
void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began) //on finger down
{
fingerDown = touch.position;
fingerUp = touch.position;
}
if (touch.phase == TouchPhase.Moved) // finger moving
{
if (!detectSwipeAfterRelease)
{
fingerUp = touch.position;
}
}
if (touch.phase == TouchPhase.Ended) //on finger off
{
fingerUp = touch.position;
DetectSwipe();
}
}
}
void DetectSwipe()
{
if (VerticalSwipeValue() > swipeThreshold && VerticalSwipeValue() > HorizontalSwipeValue())
{
if (fingerUp.y > fingerDown.y) //up
{
Jump();
}
else if(fingerUp.y < fingerDown.y) //down
{
OnSwipeDown();
}
fingerDown = fingerUp;
}
else if (HorizontalSwipeValue() > swipeThreshold && HorizontalSwipeValue() > VerticalSwipeValue())
{
if (fingerUp.x > fingerDown.x && transform.position.x < 8 ) //right
{
StartCoroutine(Move("right"));
Debug.Log("right");
}
else if (fingerUp.x < fingerDown.x && transform.position.x > -8)
{
StartCoroutine(Move("left")); //left
Debug.Log("left");
}
fingerDown = fingerUp;
}
Your answer

Follow this Question
Related Questions
Swipe to Kill Enemy (Ant) 2D 0 Answers
Touch swipe - какой метод требуется 0 Answers
Unable to use Swipe and Unity UI together 0 Answers
How to move a ball with finger gesture? 2 Answers
How do is make Unity 8-way swipe touch control code 0 Answers