I’m making a game that requires a lot of screen tapping, done very quickly. I noticed the game works fine in editor but, when I test on mobile, it slows down.
So, what is my problem exactly? When I’m tapping with 2 fingers, never at the same time but very quickly, it seems as if there is a bit of input lag. For each tap, my character sprite changes but, you can tell sometimes it takes a second to update the sprite and score. Works great in editor though.
I’m really hoping there is a way to fix this because otherwise, I’ll need to switch to an engine that can handle the amount of tapping needed.
Here is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
public Sprite jim1; // Char sprite 1
public Sprite jim2; // Char sprite 2
public int touchCount; // Amount of touches
public int colorTimer;
public GameObject cameraParent; // Camera Parent
public GameObject spaceBackground;
public SpriteRenderer sr; // Char sprite renderer
public Material spaceMaterial;
public Text scoreText; // Score text
public Text timerText; // Timer text
private bool canTap; // self explanatory
private float timer; // Countdown timer
private Color ranColor; // Random color
private Color spaceColor; // Random space color
// Use this for initialization
void Start()
{
timer = 60f; // Game timer
colorTimer = 30; // Color timer for the space background
touchCount = 0; // Setting touchcount to 0
sr = gameObject.GetComponent<SpriteRenderer>(); // Getting the sprite renderer and storing it in a variable
spaceMaterial = spaceBackground.GetComponent<MeshRenderer>().material; // Getting the mesh renderer material and storing it in a variable
scoreText.GetComponent<Text>(); // Getting the text componet
canTap = true;
}
// Update is called once per frame
void Update()
{
if (timer >= 0f) // If time is still remaining, count down
{
timer -= Time.deltaTime; // Taking 1 away every second
}
else {
timer = 0f; // If there is no time remaining, set the timer to 0
canTap = false;
}
timerText.text = Mathf.RoundToInt(timer).ToString(); // Rounding the float and then setting the timerText text value to the timers value
if (touchCount % 2 == 0) // Checking if touchcount is even or odd. If even, the sprite will be set to jim1.
{
sr.sprite = jim1;
}
else {
sr.sprite = jim2;
}
if (touchCount >= 50)
{
cameraParent.transform.position = Random.insideUnitSphere * 0.1f; // screenshake baby
}
if (touchCount >= 200)
{
transform.Rotate(Vector3.forward * Time.deltaTime * 150);
}
if (touchCount >= 250) {
gameObject.GetComponent<Animation>().enabled = true;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && canTap) // Getting screen touches and setting to score
{
touchCount++; // adding to the touchcount
scoreText.text = touchCount.ToString();
}
if (touchCount > 100)
{
colorTimer -= 1; // Take away 1 every frame
if (colorTimer <= 0)
{
ranColor = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)); // Making a new color using random values
sr.color = ranColor; // setting the sprites color to the random color
spaceColor = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1f); // Taking the random color and making it negitive
colorTimer = 30; // Resetting the timer
spaceMaterial.color = spaceColor;
}
}
}
}
**ISSUE STILL NOT RESOLVED
I think the best way to describe my issue would be, it feels like a few frames are being skipped every 7 taps or so. So, for example, each time I tap my sprite changes but, on that 7th tap, the sprite doesn’t change.
**