亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Unity C# 在 android 上點擊和滑動

Unity C# 在 android 上點擊和滑動

C#
Cats萌萌 2022-01-09 14:43:07
我目前正在編寫一個與障礙物統一的簡單游戲,這是一個無休止的游戲,玩家可以跳躍、左右移動來避開這些障礙物。該游戲適用于華碩和華為設備,但不適用于三星。使用三星設備時,當我點擊屏幕時播放器不會跳躍,但滑動有效。我的代碼: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() 函數中調用。
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

由于我是新來的,我還不能使用評論來提出更多問題。使用三星時到底什么不起作用?


不過,您可以簡化計算。


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

如果游戲僅使用水平滑動,則不需要向量來存儲和計算滑動增量。


如果您提供更多信息,我很樂意提供更多幫助。


查看完整回答
反對 回復 2022-01-09
  • 1 回答
  • 0 關注
  • 400 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號