【问题标题】:Unity C# tap and swipe on androidUnity C# 在 android 上点击和滑动
【发布时间】:2018-09-08 12:15:42
【问题描述】:

我目前正在编写一个带有障碍物的简单游戏,这是一个无休止的游戏,玩家可以跳跃、左右移动来避开这些障碍物。该游戏适用于华硕和华为设备,但不适用于三星。对于三星设备,当我点击屏幕时播放器不会跳跃,但滑动有效。

我的代码:

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】:

    由于我是新来的,我还不能使用 cmets 来询问更多问题。 使用三星时究竟是什么不起作用?

    你可以简化计算。

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

    仅比较 > 或

                case TouchPhase.Began:
    
                  startPos = touch.position.x; // startPos could be float
    
                break;
    

    因此,

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

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

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

    【讨论】:

    • 感谢您的回复。我稍后会尝试你的代码。在三星中,玩家不会跳跃(所以点击不起作用)但他可以左右移动(所以滑动有效)
    • 我没有看到 minSwipeDistX 的定义,但我认为它应该是相对于屏幕尺寸的,而不是所有设备的恒定值。类似于 Screen.width 的一小部分。 minSwipeDistX 对于该设备来说可能只是一个太低的值,并且在点击屏幕时手指会在该范围内移动。也许你可以试试 TouchPhase.Stationary。
    • 我是这样定义的:public float minSwipeDistX;使用 TouchPhase.Stationary 时问题仍然存在
    • 好的。分配给 minSwipeDistX 的值是多少?
    • 未设置。它应该等于屏幕宽度吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    相关资源
    最近更新 更多