Unity: FlappyBird controls don't working on Android, how should I do this?

450 views Asked by At

I made a Flappy Bird-like game in Unity 2D for Android. I's working & running on my device, but there is one problem with the contsols:If i tap the screen the bird flaps and flyes, but if I hold the screen the bird is keep going up and up.. I want to only sense one tap. There is my code:

        if (Input.touchCount == 1) {
            didFlap = true;
        }

Full code:

using UnityEngine;
using System.Collections;

public class BirdMovement : MonoBehaviour {

    Vector3 velocity = Vector3.zero;
    public float flapSpeed    = 100f;
    public float forwardSpeed = 1f;

    bool didFlap = false;

    Animator animator;

    public bool dead = false;
    float deathCooldown;

    public bool godMode = false;

    // Use this for initialization
    void Start () {
        animator = transform.GetComponentInChildren<Animator>();

        if(animator == null) {
            Debug.LogError("Didn't find animator!");
        }
    }

    // Do Graphic & Input updates here
    void Update() {
        if(dead) {
            deathCooldown -= Time.deltaTime;

            if(deathCooldown <= 0) {
                if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
                    Application.LoadLevel( Application.loadedLevel );
                }
            }
        }
        else {
            if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
                didFlap = true;
            }
            if (Input.touchCount == 1) {
                didFlap = true;
            }

        }
    }


    // Do physics engine updates here
    void FixedUpdate () {

        if(dead)
            return;

        rigidbody2D.AddForce( Vector2.right * forwardSpeed );

        if(didFlap) {
            rigidbody2D.AddForce( Vector2.up * flapSpeed );
            animator.SetTrigger("DoFlap");


            didFlap = false;
        }

        if(rigidbody2D.velocity.y > 0) {
            transform.rotation = Quaternion.Euler(0, 0, 0);
        }
        else {
            float angle = Mathf.Lerp (0, -90, (-rigidbody2D.velocity.y / 3f) );
            transform.rotation = Quaternion.Euler(0, 0, angle);
        }
    }

    void OnCollisionEnter2D(Collision2D collision) {
        if(godMode)
            return;

        animator.SetTrigger("Death");
        dead = true;
        deathCooldown = 0.5f;
    }
}
1

There are 1 answers

2
Aodai Irshed On BEST ANSWER

If you wanna do it with touchcount I think you should do something like this create bool variable in start cangoup=true Ask

if (input.touchcount==0) 
   Cangoup=true;

And the didflap statement should be something like this If(cangoup) {//everything you wrote Cangoup=false;} I wrote this from my iphone so there may be few mistakes but i am sure you can fix them and i think this will do you good if it doesn't just comment on the answer and ill reply