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();
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; } } } }
}