Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sokaz Games · Dec 09, 2015 at 04:10 PM · c#androidinputslowinput.getmousebuttondown

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zgub4 · Dec 09, 2015 at 05:28 PM 0
Share

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.

avatar image tanoshimi zgub4 · Dec 09, 2015 at 08:50 PM 0
Share

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.

avatar image Sokaz Games · Dec 09, 2015 at 09:26 PM 0
Share

I had tried that solution aswell but, it only got slightly better.

avatar image tanoshimi Sokaz Games · Dec 09, 2015 at 09:57 PM 0
Share

Well we can't help if you don't post your code...

avatar image Sokaz Games tanoshimi · Dec 09, 2015 at 10:13 PM 0
Share

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();
          }
      }
Show more comments
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fffMalzbier · Dec 10, 2015 at 09:55 AM 0
Share

Good Point with the profiler.

avatar image Sokaz Games · Dec 15, 2015 at 06:45 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sokaz Games · Dec 15, 2015 at 10:42 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges