- Home /
Input.GetMouseButtonDown() slow on mobile
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.
Answer by fffMalzbier · Dec 09, 2015 at 04:13 PM
You should not use the mouse functions on mobile platforms.
Use the touch functions of the input class.
http://docs.unity3d.com/ScriptReference/Input-touchCount.html http://docs.unity3d.com/ScriptReference/Input-touches.html http://docs.unity3d.com/ScriptReference/Touch.html
Why is that? Could you explain this please, since mouse functions work just fine, but I'm interested how do you replace $$anonymous$$ouseButton, $$anonymous$$ouseButtonDown and $$anonymous$$ouseButtonUp with Touch.
The reason not to use mouse buttons on mobile is that your mobile device doesn't have a mouse... currently, the first touch does simulate mouse events, but that is undocumented behaviour so may break at any time. Loop through Input.touches and look at the touchPhase to deter$$anonymous$$e which fingers have been placed/moved/lifted each frame.
I had tried that solution aswell but, it only got slightly better.
Well we can't help if you don't post your code...
I'm using if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){}
All the code on the inside is the exact same as before.
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) // Getting screen touches and setting to score
{
Color ranColor = new Color(Random.Range(0f,1f), Random.Range(0f,1f), Random.Range(0f,1f)); // $$anonymous$$aking a new color using random values
Color negColor = new Color(1-ranColor.r, 1-ranColor.g, 1-ranColor.b, 1f); // Taking the random color and making it negitive
sr.color = ranColor; // setting the sprites color to the random color
if (touchCount >= 100) { // If touchcount is over 100
space$$anonymous$$aterial.color = negColor; // Set the space background to the negitive color
}
touchCount++; // adding to the touchcount
scoreText.text = touchCount.ToString();
}
}
Answer by Yury-Habets · Dec 10, 2015 at 08:36 AM
First of all, as said by @fffMalzbier, don't use mouse functions on mobile, there should be a warning about that in the Editor when you build.
Second, the touches are processed once per frame.
Third, there's a profiler built in, which can shed some light on why you are experiencing lag.
So, you're saying you checked my code and it seemed fine, right? So the problem must be somewhere else?
"Second, the touches are processed once per frame."
I'll check the profiler.
Answer by ArshakKroyan · Dec 15, 2015 at 09:51 PM
Hi @Sokaz Games I also have had the same problem and everything is very fine using touches. Also when quickly tapping on the screen unity throws right mouse button down event instead of left mouse button.
I've been using touches actually and, while it has improved, I do still come across the issue. I have updated my code at the top.
Your answer
![](https://koobas.hobune.stream/wayback/20220612051917im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Input.GetTouch(0).position.x and TouchPhase.Began 1 Answer
Android Plugin Help 1 Answer
Android Gyroscope not working ? 2 Answers
Touch Input with Event Trigger. How to handle with it properly. 0 Answers
Getting My Character to Move 1 Answer