Hello,
I am working on a mobile game, and so I use swipes to fling a circle around the screen.
However, sometimes, when I swipe fast, the touch controls don’t work. It registers only some of the swipes.
This sort of issue doesn’t happen when using other apps, and I even turned on the visual touch feedback on my phone to confirm that it’s not the problem. The visual feedback was showing yet my app didn’t respond.
Is there anything I can do to fix it?
Here is the swipe detection code in the circle’s script:
Note: the touchMoveCount and touchMoveLimit variables are used to get a more precise direction of each swipe, as the direction is not calculated when the touch position first moves.
Vector2 startPos;
public float dashVelocity;
bool hasDashed;
int touchMoveCount;
public int touchMoveLimit;
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
touchMoveCount = 0;
hasDashed = false;
break;
case TouchPhase.Moved:
touchMoveCount++;
if (!hasDashed && touchMoveCount >= touchMoveLimit)
{
Vector2 direction = (touch.position - startPos).normalized;
rb.velocity = direction * dashVelocity;
hasDashed = true;
}
break;
}
}
}