• 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 vmnoodel · Feb 13, 2015 at 05:36 AM · touchresponse

My tap tap game - facing touch response issues

Hello.

I have made a tap tap jump kind of game for mobile devices . Recently I am getting a lot of bad reviews stating that the game touch controls are not responsive enough . I am sharing my code here (which I believe is flawlesssly near perfect) . Touch response is very critical in this game (a matter of life and death) for the game character . Also my game 2D and runs at solid 400+fps .

 void Update(){
 
         if (!isDead) {
             DetectTap();
 
         }
 }
 
 public void DetectTap(){
 
     
         //detect single tap
         if (Input.touchCount == 1) {
 
                         if (Input.GetTouch(0).tapCount == 1) {
                                 if (grounded) 
                                         jumpCommand = true;
                                         //jump ();
                         } else if (Input.GetTouch(0).tapCount == 2) {
                                 if (!hasDoubleJumped && !grounded){
                                         jumpCommand  = false;
                                         doubleJumpCommand = true;
                                     }
                         }
                 }
     
     }
 
 
 


UPDATE - - The game runs on 400 to 800 fps in Unity Editor - My bad - It might be running at lower fps on mobile devices

  • Also below I have attached the complete PlayerScript.cs

    public class PlayerScript : MonoBehaviour {

       /*
          * 1.Default player motion move forward
          * 2.Single tap jump player
          * 3.Double tap double jump player
          * 4.On collision with danger
          *  i.stop animation
          *  ii.stop game
          *  iii.change player sprite to dead
          * 
          * Animations:
          *  - default anim is freeze
          *  - running anim when game start
          *  - dead anim when hit with cactus
          */
     
         public TextMesh debugText;
         private bool grounded = true;
         private Animator dinoAnim;
     
         public int dinoSpeedStepper = 2;
         private AudioSource a;
         public AudioClip jumpAudio;
         public AudioClip fartAudio;
         public AudioClip deadClipAudio;
     
         public static bool isRestart = false;
     
             public static bool isDead = false;
         
     void getAudio(){
             a = this.GetComponent<AudioSource>();
             if (a == null)
                             debugText.text = "no audio source";
                     else
                             debugText.text = "got audio source";
         }
     
         void OnCollisionEnter2D(Collision2D other){
             //debugText.text = other.gameObject.name.ToString();
     
             if (other.gameObject.name.ToString().Equals("Ground")) {
                 hasDoubleJumped = false;
     
                 grounded = true; 
                 dinoSpeedForward = 1f;
                 dinoAnim.SetBool("jump",false);
                 dinoAnim.SetBool("run",true);
             }
     
             if (other.gameObject.tag.ToString ().Equals ("Danger")) {
                 //return;
                 isDead = true;
                 dinoSpeedForward = 0f;
                 transform.position = Vector3.MoveTowards(transform.position
                                                          ,other.transform.position
                                                          ,.1f);
     
                 transform.rigidbody2D.isKinematic = true;
                 dinoAnim.SetBool("run",false);
                 dinoAnim.SetBool("jump",false);
                 dinoAnim.SetBool("dead",true);
     //            Handheld.Vibrate();
     
                 a.PlayOneShot(deadClipAudio);
     
             }
                     
         }
     
     
     
     
         void Start(){
             isDead = false;
             debugText.text = "in debug mode";
             dinoAnim = this.gameObject.GetComponent<Animator>();
             getAudio();
         
         }
     
         
     
         
     
         void Update(){
     
             if (!isDead) {
                 DetectTap();
     
             }
             
     #if UNITY_EDITOR
     
             if (Input.GetKeyDown(KeyCode.Space)) {
                 if(isDead)
                     return;
                 if(grounded)
                 {
                     jumpCommand = true;
                 }else{
                     jumpCommand  = false;
                     doubleJumpCommand = true;
     
                 }
             }
     
     
     #endif
     
     
         
         }
     
         public bool jumpCommand = false;
         public bool doubleJumpCommand = false;
     
         void FixedUpdate(){
             MoveForward();
     
             if (jumpCommand) {
                             grounded = false;
                             jump ();
                             jumpCommand = false;
                     } else if (doubleJumpCommand) {
                             
                             doubleJump();
                             doubleJumpCommand = false;
                     }
     
     
         }
     
     
     
         
     
     
         private void doubleJump(){
     
             grounded = false;
             if(!hasDoubleJumped)
             {
                 hasDoubleJumped = true;
                 dinoSpeedForward = 1.4f;
             
                 rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x,5.3f);
             }
         }
         
         private void jump(){
             grounded = false;
             dinoSpeedForward = 1.01f;
             
             rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x,5);
             a.PlayOneShot(jumpAudio,1f);
             dinoAnim.SetBool("jump",true);
             dinoAnim.SetBool("run",false);
         }
     
         public float dinoSpeedForward = 1f;
     
     
     
         public void MoveForward(){
     
             this.gameObject.transform.Translate (Vector3.right*Time.deltaTime*dinoSpeedForward*dinoSpeedStepper * 1.1f);
         }
     
     
     
         public void DetectTap(){
     
             if (Input.touchCount == 1) {
     
                             if (Input.GetTouch(0).tapCount == 1) {
                                     if (grounded) 
                                             jumpCommand = true;
                                             
                             } else if (Input.GetTouch(0).tapCount == 2) {
                                     if (!hasDoubleJumped && !grounded){
                                             jumpCommand  = false;
                                             doubleJumpCommand = true;
                                         }
                             }
                     }
         
         }
     
     
     
     }
     
    
    
    
    
    
Comment
Add comment · Show 2
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 tanoshimi · Feb 13, 2015 at 05:41 AM 0
Share

400fps on a mobile device? Are you sure?!

You shouldn't call Input.GetTouch(0).tapCount in both branches of your if statement - just call it once per frame and cache the result. And is that really the only code that runs in Update()? Where do you set grounded? Does it use raycasting?

avatar image vmnoodel · Feb 13, 2015 at 06:06 AM 0
Share

no I am not using any raycasts . I am using simple Collision detection in script to make grounded = true . Also I have updated the question with complete code . I will cache the tapcount to see if it helps reduce touch lag . Need more optimization tips please refer code .

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Feb 13, 2015 at 06:43 AM

Hint: you're definitely not getting 400fps on a mobile device :) You really need to profile on the target hardware to get reliable performance tests.

Looking at your code, I'd guess Audio.PlayOneShot() might be the culprit; it's notoriously laggy on certain mobiles. Have you tried commenting those lines out? Are you using uncompressed WAVs? Have you tried changing the DSP buffer size?

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 vmnoodel · Feb 13, 2015 at 09:46 AM 0
Share

I am gonna do those things you mentioned . Will take a while .

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How do I render Oculus Touch controllers in-game in Unity? 1 Answer

Mouse and Oculus Touch Controller Inputs 0 Answers

I want to rotate a object on double tap ,Can any One help me with this? 2 Answers

Do UI Buttons work the same for Touch for an Android Game? 1 Answer

Rotate camera based on left/right swipe 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges