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;
    }
}
4

1 回答 1

1

如果你想用 touchcount 来做,我认为你应该做这样的事情 create bool variable in start cangoup=true Ask

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

并且 didflap 声明应该是这样的 If(cangoup) {//everything you write Cangoup=false;} 我是用我的 iphone 写的,所以可能会有一些错误,但我相信你可以修复它们,我认为这会做如果它不只是评论答案并且回复不好,那你很好

于 2014-12-06T22:37:17.040 回答