0

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;
    }
}
Steven
  • 166,672
  • 24
  • 332
  • 435

1 Answers1

1

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

apxcode
  • 7,696
  • 7
  • 30
  • 41
Aodai Irshed
  • 230
  • 1
  • 11
  • Thanks for the answer, but there is one problem: I tried this: if touchCount == 1 --> cangoup = false and if touchCount == 1 --> cangoup = true I changed all didFlap to cangoup. I want the game to run both on PC and Android, but if i leave the if touchCount == 1 --> cangoup = false and if touchCount == 1 --> cangoup = true in the code the bird shoots out to the sky because if touchCount == 0 the cangoup is changes to true and this happens: if(cangoup) { rigidbody2D.AddForce( Vector2.up * flapSpeed ); animator.SetTrigger("DoFlap"); cangoup = false; } Sorry for my bad English:) – Tamás Erdős Dec 07 '14 at 10:30
  • No that not what i meant you should leave did flap alone you just add this to your existing code don't change it in your update if(input.touchcount==0) cangoup=true; and if (didflap) you put in it if(cangoup){your code and cangoup=false;} don't delete anything or change anything from your cose just add these – Aodai Irshed Dec 07 '14 at 10:41