Unity C#在Android上点按并轻扫

问题描述 投票:0回答:1

我目前正在与障碍团结一致的简单游戏,它是一个无尽的游戏,玩家可以跳跃,向右和向左移动以避免这些障碍。该游戏适用于华硕和华为设备,但三星没有。使用三星设备时,当我点击屏幕但是滑动工作时,播放器不会跳转。

我的代码:

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

在update()函数中调用此函数。

谢谢

c# unity3d swipe
1个回答
0
投票

由于我是新来的,我不能用评论来提出进一步的问题。使用三星时究竟什么不起作用?

您可以简化计算。

                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();

                }

只比较>或<0时,不需要Math.Sign。

            case TouchPhase.Began:

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

            break;

因此,

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

如果游戏仅使用水平滑动,则不需要矢量来存储和计算滑动增量。

如果您提供更多信息,我很乐意提供更多帮助。

© www.soinside.com 2019 - 2024. All rights reserved.