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 GamezAtWork · Jul 29, 2011 at 04:01 AM · androidiphonetouchlag

Massive Touch Lag on mobile devices?

Hi guys, I have this real problem with Touch Screen Lag on all the devices...

After a while of playing my game, the Touch sensing will suddenly lag greatly, resulting in me being unable to interact with the game properly. Strangely enough, the game still runs as smoothly as before... ---> Is there any way I can fix this Touch screen lag? Its a major problem in the game, and we can't get it out unless we solve this...

Also, when we export it to the iPad, it somehow lags alot more than on the iPhone or the Android... ---> Is there a reason for this? And is there a way we can fix it?

Please help me, its exceedingly important...

This is the code which makes the touch work by the way.

function iPhoneUpdate(){ // If there are any touching on the screen

 // Look for new touches
     for (var i:Number = 0; i < Input.touchCount; i++){
         var theTouch:Touch = Input.GetTouch(i);
         if (!touchEvents.Contains(theTouch.fingerId)){
             touchEvents.Add(theTouch.fingerId,new touchEvent(theTouch.position,theTouch.tapCount));
             var touchRay = mainCamera.ScreenPointToRay(theTouch.position);
             if (Physics.Raycast(touchRay,hit)){
                 if (hit.transform.tag == "Roach"){
                     var newTouchedObject:touchedObject = new touchedObject();
                     newTouchedObject.theObject = hit.transform.gameObject;
                     newTouchedObject.pointStarted = theTouch.position;
                     theTouchEvent = touchEvents[theTouch.fingerId];
                     theTouchEvent.theTouchedObject = newTouchedObject;
                 }
             }
         }
         else {
             theTouchEvent = touchEvents[theTouch.fingerId];
             theTouchEvent.checked = true;
             theTouchEvent.currentPosition = theTouch.position;
         }
     }
     
     
     textToWrite = "";
     
     // Update current touch events
     for (var theTouchID in touchEvents.Keys){
         
         var theTouchEvent:touchEvent = touchEvents[theTouchID];
         
         // If that touch has ended
         if (theTouchEvent.checked == false){
             
             
             
             // If anything was touched
             if (theTouchEvent.theTouchedObject){
                 
                 // Update last position
                 theTouchEvent.theTouchedObject.pointEnded = theTouchEvent.currentPosition;
             
                 // If the touch was very short
                 if (theTouchEvent.currentTime < tapTime){
                     // TOUCH PHASE WAS A TAP
                     NotificationCenter.DefaultCenter().PostNotification(theTouchEvent.theTouchedObject.theObject,"Squash",theTouchEvent);
                 
                 }
                 // If it wasn't short
                 else {
                     // TOUCH PHASE WAS A FLICK
                     NotificationCenter.DefaultCenter().PostNotification(theTouchEvent.theTouchedObject.theObject,"Flicked",theTouchEvent);

                 }
             }
             
             
         }
         
         // If that touch has not ended yet
         else{
             
             // For tracing
             //textToWrite += theTouchID +" : " + theTouchEvent.currentTime + "\n";
             
             // Increase timer for touch
             theTouchEvent.currentTime += Time.deltaTime;
             
             
             
         }
     }
     
     // Delete old touch events
     
     var newTouchEvents:Hashtable = touchEvents.Clone();
     
     for (var theTouchIDRemove in touchEvents.Keys){
         
         theTouchEvent = touchEvents[theTouchIDRemove];
         
         if (theTouchEvent.checked == false){
             newTouchEvents.Remove(theTouchIDRemove);
         }
         
     }
     
     touchEvents = newTouchEvents;
     
     
     for (var theTouchIDDirty in touchEvents.Keys){
         theTouchEvent = touchEvents[theTouchIDDirty];
         theTouchEvent.checked = false;
     }
     
     /*
     var theGUIText:GUIText = transform.GetComponent(GUIText);
     if (textToWrite != ""){
         theGUIText.text = textToWrite;
     }
     else {
         theGUIText.text = "Nothing here";
     }*/

}

P.S Strangely enough, on the Android, the game runs alot smoother at the start, but after around 5-10 minutes, the touch screen lag gets to such a terrible point I can't even play... On the iPhone3GS, it runs not as smooth, but takes around 10-15 minutes for the lag to be too terrible to play...

EDIT

Fixed up the code a bit, here it is.

 function iPhoneUpdate(){
 // If there are any touching on the screen
 
 // Look for new touches
     for (var i:Number = 0; i < Input.touchCount; i++){
         theTouch = Input.GetTouch(i);
         if (!touchEvents.Contains(theTouch.fingerId)){
             touchEvents.Add(theTouch.fingerId,new touchEvent(theTouch.position,theTouch.tapCount));
             touchRay = mainCamera.ScreenPointToRay(theTouch.position);
             if (Physics.Raycast(touchRay,hit)){
                 if (hit.transform.tag == "Roach"){
                     newTouchedObject = new touchedObject();
                     newTouchedObject.theObject = hit.transform.gameObject;
                     newTouchedObject.pointStarted = theTouch.position;
                     theTouchEvent = touchEvents[theTouch.fingerId];
                     theTouchEvent.theTouchedObject = newTouchedObject;
                 }
             }
         }
         else {
             theTouchEvent = touchEvents[theTouch.fingerId];
             theTouchEvent.checked = true;
             theTouchEvent.currentPosition = theTouch.position;
         }
     }
     
     
     textToWrite = "";
     
     // Update current touch events
     if (touchEvents.Count > 0){
         for (var theTouchID in touchEvents.Keys){
             
             theTouchEvent = touchEvents[theTouchID];
             
             // If that touch has ended
             if (theTouchEvent.checked == false){
                 
                 
                 
                 // If anything was touched
                 if (theTouchEvent.theTouchedObject){
                     
                     // Update last position
                     theTouchEvent.theTouchedObject.pointEnded = theTouchEvent.currentPosition;
                 
                     // If the touch was very short
                     if (theTouchEvent.currentTime < tapTime){
                         // TOUCH PHASE WAS A TAP
                         NotificationCenter.DefaultCenter().PostNotification(theTouchEvent.theTouchedObject.theObject,"Squash",theTouchEvent);
                     
                     }
                     // If it wasn't short
                     else {
                         // TOUCH PHASE WAS A FLICK
                         NotificationCenter.DefaultCenter().PostNotification(theTouchEvent.theTouchedObject.theObject,"Flicked",theTouchEvent);
 
                     }
                 }
                 
                 
             }
             
             // If that touch has not ended yet
             else{
                 
                 // For tracing
                 //textToWrite += theTouchID +" : " + theTouchEvent.currentTime + "\n";
                 
                 // Increase timer for touch
                 theTouchEvent.currentTime += Time.deltaTime;
                 
                 
                 
             }
         }
         
         // Delete old touch events
         
         newTouchEvents = new Hashtable();
         
         for (var theTouchIDRemove in touchEvents.Keys){
             
             theTouchEvent = touchEvents[theTouchIDRemove];
             
             if (theTouchEvent.checked == true){
                 theTouchEvent.checked = false;
                 newTouchEvents.Add(theTouchIDRemove,touchEvents[theTouchIDRemove]);
             }
             
         }
         
         touchEvents = newTouchEvents;
     }

}

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
2

Answer by Dreamora · Jul 29, 2011 at 06:16 AM

You are creating a large amount of garbage in this code, with cloning the hashtable, creating new objects and similar things.

Due to that, it will once it hits the memory limit of the device, start to lag considerably, as the garbage collection will start to eat all frametime at worst. If you own iOS Pro / Android Pro you can see that in the Profiler as "Overhead", on iOS you can alternatively also enable the in code profiler that prints to the console, this one will show it directly as time for the garbage collecting.

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 GamezAtWork · Jul 29, 2011 at 08:02 AM 0
Share

Thanks! The problem where it gets slower later is kinda better now. But... How do I solve the touch lag?

avatar image
0

Answer by GamezAtWork · Aug 01, 2011 at 01:34 AM

Can anyone help with the touch lag issue...?

Comment
Add comment · 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
0

Answer by guoliulong · Oct 26, 2012 at 04:02 AM

I have the same problem,even if using a empty scene!!!!!

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Touch Hold for Power 1 Answer

How do I prevent / deal with input lag on a touch interface? 3 Answers

InputManager vs iPhone/Android 2 Answers

2D Camera (Android, Iphone) 1 Answer

Detecting Touch on Iphone and Android 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