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

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

如何不斷獲取觸摸位置?

如何不斷獲取觸摸位置?

C#
皈依舞 2022-07-10 10:06:59
我有一個代碼可以預測LateUpdate(). 它可以在 PC 和鼠標上完美運行。下面是使用鼠標時的代碼:    if(Input.GetMouseButton(0)){        //get the rotation based on the start drag position compared to the current drag position        zRotation = (Input.mousePosition.y - mouseStart) * (manager.data.sensitivity/15);        zRotation = Mathf.Clamp(zRotation, -manager.data.maxAimRotation, manager.data.maxAimRotation);    }    //reset the rotation if the player is not aiming    else if((int) zRotation != 0){        if(zRotation > 0){            zRotation --;        }        else{            zRotation ++;        }    }我現在想把它移植到 Android 上,所以我在玩Input.Touch. 我將上面的代碼更改為以下內容: if (Input.touchCount > 0)            {                //get the rotation based on the start drag position compared to the current drag position                zRotation = (Input.GetTouch(0).deltaPosition.y) * (manager.data.sensitivity / 15);                zRotation = Mathf.Clamp(zRotation, -manager.data.maxAimRotation, manager.data.maxAimRotation);            }            //reset the rotation if the player is not aiming            else if ((int)zRotation != 0)            {                if (zRotation > 0)                {                    zRotation--;                }                else                {                    zRotation++;                }            }但是zRotation它不起作用,因為它在鼠標中起作用。它在每一幀后不斷重置到起始位置。它幾乎看起來像抖動。我究竟做錯了什么?
查看完整描述

1 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

我看到您正在使用的移動控件Input.GetTouch(0).deltaPosition.y。但是,為您提供上次更新deltaPosition位置與當前位置之間的差異。因此,假設您的應用程序以每秒 60 幀的速度運行,它將返回每 1/60 秒的距離。當然,這將是一個接近于零的數字,我相信這就是為什么它看起來總是返回起始位置的原因


https://docs.unity3d.com/ScriptReference/Touch-deltaPosition.html


您將不得不以類似于使用鼠標方法的方式進行操作。將變量設置為touchStartonTouchphase.Began并將其與touch.position.


float touchStart = 0;


if (Input.touchCount > 0)

            {

                if (Input.GetTouch(0).phase == TouchPhase.Began) touchStart = Input.GetTouch(0).position.y;

                //get the rotation based on the start drag position compared to the current drag position

                zRotation = (Input.GetTouch(0).position.y - touchStart) * (manager.data.sensitivity / 15);

                zRotation = Mathf.Clamp(zRotation, -manager.data.maxAimRotation, manager.data.maxAimRotation);

            }

            //reset the rotation if the player is not aiming

            else if ((int)zRotation != 0)

            {

                if (zRotation > 0)

                {

                    zRotation--;

                }

                else

                {

                    zRotation++;

                }

            }

不過我還沒有測試過,如果我錯了,請糾正我!


查看完整回答
反對 回復 2022-07-10
  • 1 回答
  • 0 關注
  • 87 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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