- Home /
[SOLVED] Touch Controls Not Responsive
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;
}
}
}
Answer by LTonon · Oct 13, 2019 at 12:30 AM
Probably this is due to you checking for Input on FixedUpdate. Usually it's best to do check for Input on Update since it is called more often than FixedUpdate and thus would avoid this feeling of your game being unresponsive. So basically you would move your entire code to an Update method and leave only the rb.velocity part in the FixedUpdate
Maybe something like this (not tested):
private Vector2 direction;
void Update()
{
/* Not sure if this works, you gotta find a way to reset the direction to
avoid it being always the same as the previous input */
direction = Vector2.zero;
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)
{
direction = (touch.position - startPos).normalized;
hasDashed = true;
}
break;
}
}
}
private void FixedUpdate
{
rb.velocity = direction * dashVelocity;
}
It fixed it, Thank you :). I used a slightly different method than your suggestion, but after separating the input detection from the FixedUpdate method, it works fluently.