- Home /
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; } } } } }
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?
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 .
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?
I am gonna do those things you mentioned . Will take a while .
Your answer
Follow this Question
Related Questions
Camera momentum after lifting finger 0 Answers
Detect the swipe and add force respective to it? 3 Answers
Detect touch on a 2D plane with a transparent image? 1 Answer
An Object Following Touch Position Problem 3 Answers
Custom touch phase? 0 Answers