Unity C# 在 android 上点击和滑动

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


我的代码:


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() 函数中调用。


Cats萌萌
浏览 374回答 1
1回答

泛舟湖上清波郎朗

由于我是新来的,我还不能使用评论来提出更多问题。使用三星时到底什么不起作用?不过,您可以简化计算。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float swipeValue = touch.position.x - startPos.x; //Mathf.Sign(touch.position.x - startPos.x);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (swipeValue > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {//right swipe&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anim.Play(change_Line_Animation);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.localPosition = second_PosOfPlayer;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoundManager.instance.PlayMoveLineSound();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (swipeValue < 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {//left swipe&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anim.Play(change_Line_Animation);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.localPosition = first_PosOfPlayer;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SoundManager.instance.PlayMoveLineSound();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }仅比较 > 或 < 小于 0 时不需要 Math.Sign。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TouchPhase.Began:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startPos = touch.position.x; // startPos could be float&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;因此,float swipeDistHorizontal = Mathf.Abs(touch.position.x - startPos);如果游戏仅使用水平滑动,则不需要向量来存储和计算滑动增量。如果您提供更多信息,我很乐意提供更多帮助。
打开App,查看更多内容
随时随地看视频慕课网APP