Multi-touch bug on my first Android game

Right now I've been developing a game which will make the character move up or down if either swipe up or down on the left side of the screen. Then, he can also, shoot projectiles if I tap the right side of the screen. I've succeeded on making it work but the problem is that the swipe function do not works while I tap the the right screen. Both only works if I do one of them. I cannot move the character if I am firing projectiles. Any suggestions? Appreciate it in advanced.

Here's the code:

For the swipe movement function:

void Swipe() {

    if (Input.touchCount > 0) {

        Touch t = Input.GetTouch(0);

        if (t.phase == TouchPhase.Began)
        {

            lp = t.position;
            fp = t.position;
            text.text = fp.x.ToString();
        }
        else if (t.phase == TouchPhase.Ended)
        {

            lp = t.position;

            //Swipe Up
            if (fp.y < lp.y && fp.x < 400)
            {


                currentLane += 1;


            }
            //Swipe Down
            else if (fp.y > lp.y && fp.x < 400)
            {

                currentLane -= 1;

            }
        }



    }

}

And here's the code for the tap or firing projectiles function:

    void FireBullets() {

    interval += 2 * Time.deltaTime;

    anim.SetBool("Attacking", firing);


    if (Input.touchCount > 0 && interval > .75f) {


        Vector3 bulletTouchPos;

        Touch bulletT = Input.GetTouch(0);
        bulletTouchPos = bulletT.position;

        if (bulletT.phase == TouchPhase.Began) {

            if (bulletTouchPos.x > 400)
            {

                interval = 0;
                firing = true;
                //Fire Bullets
                Instantiate(bullets, new Vector3(transform.position.x + 1.2f, transform.position.y + .3f, transform.position.z), Quaternion.identity);
            }
        }


    }else
    {

       firing = false;

    }


}

Upvotes: 0

Views: 70

Answers (1)

Your lp and fp values don't care which finger is being checked.

If you want to detect multiple touch gestures at once, you need to discriminate your detection based on which finger that is.

You can do this by looking at the Touch.fingerId value, which is unique for each finger. Fingers are just an ID assigned to a given touching point as it moves across the screen in order to identify it from other touching points currently also on the screen.

You'll need to adjust your code to handle how to store the information you need in order to do what you need, but this should get you started.

Upvotes: 1

Related Questions