简体   繁体   English

在Android上点击并滑动Unity C#

[英]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. 在update()函数中调用此函数。

Thank you 谢谢

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. 仅比较>或<小于0时,不需要Math.Sign。

            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. 如果您提供更多信息,我们很乐意为您提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM