为了账号安全,请及时绑定邮箱和手机立即绑定

如何不断获取触摸位置?

如何不断获取触摸位置?

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 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号