th3gr3yman
th3gr3yman

Reputation: 21

Unity C# tap and swipe on android

I'm currently wiritng a simple game in unity with obstacles, it is an endless game and the player can jump, move to right and left in order to avoid these obstacles. The game works with asus and huawei devices but on samsung doesn't. With samsung devices the player doesn't jump when I tap the screen but the swipe works.

My code:

void change()
{
    if (Input.touchCount > 0)

    {

        Touch touch = Input.touches[0];



        switch (touch.phase)

        {

            case TouchPhase.Began:

                startPos = touch.position;

                break;



            case TouchPhase.Ended:



                float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;

                if (swipeDistHorizontal > minSwipeDistX)

                {

                    float swipeValue = Mathf.Sign(touch.position.x - startPos.x);

                    if (swipeValue > 0)
                        {//right swipe

                        anim.Play(change_Line_Animation);
                        transform.localPosition = second_PosOfPlayer;

                        SoundManager.instance.PlayMoveLineSound();

                    }
                    else if (swipeValue < 0)
                    {//left swipe

                        anim.Play(change_Line_Animation);
                        transform.localPosition = first_PosOfPlayer;

                        SoundManager.instance.PlayMoveLineSound();

                    }


                }

                else {

                      if (!player_Jumped){

                          anim.Play(jump_Animation);
                          player_Jumped = true;

                          SoundManager.instance.PlayJumpSound();
                      }

                }

                break;
        }
    }
}

This function is called in the update() function.

Thank you

Upvotes: 0

Views: 971

Answers (1)

Marek S.
Marek S.

Reputation: 118

As I am new here, I cannot use comments to ask for further questions yet. What exactly does not work when using Samsung?

You could simplify the computations though.

                float swipeValue = touch.position.x - startPos.x; //Mathf.Sign(touch.position.x - startPos.x);

                if (swipeValue > 0)
                    {//right swipe

                    anim.Play(change_Line_Animation);
                    transform.localPosition = second_PosOfPlayer;

                    SoundManager.instance.PlayMoveLineSound();

                }
                else if (swipeValue < 0)
                {//left swipe

                    anim.Play(change_Line_Animation);
                    transform.localPosition = first_PosOfPlayer;

                    SoundManager.instance.PlayMoveLineSound();

                }

You do not need Math.Sign when only comparing to > or < than 0.

            case TouchPhase.Began:

              startPos = touch.position.x; // startPos could be float

            break;

and hence,

float swipeDistHorizontal = Mathf.Abs(touch.position.x - startPos);

If the game uses only horizontal swiping, you do not need vectors to store and compute swipe delta.

If you provide more information, I am happy to help more.

Upvotes: 1

Related Questions