如何不断获取触摸位置?
How to constantly get the touch position?
我有一个代码可以预测 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
不能像在鼠标中那样工作。它会在每一帧之后不断重置到起始位置。它看起来几乎像在抖动。
我做错了什么?
我看到您使用的是移动控件 Input.GetTouch(0).deltaPosition.y
。然而,deltaPosition
给出了上次 update 与当前位置之间的差异。因此,假设您的应用 运行 每秒 60 帧,它将 return 每 1/60 秒的距离。当然,这将是一个接近于零的数字,我相信这就是为什么它看起来总是 return 的起始位置
https://docs.unity3d.com/ScriptReference/Touch-deltaPosition.html
您必须按照与使用鼠标方法进行操作类似的方式进行操作。在 Touchphase.Began
上设置变量 touchStart
并将其与 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++;
}
}
虽然我还没有测试过,如果我错了请纠正我!
我有一个代码可以预测 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
不能像在鼠标中那样工作。它会在每一帧之后不断重置到起始位置。它看起来几乎像在抖动。
我做错了什么?
我看到您使用的是移动控件 Input.GetTouch(0).deltaPosition.y
。然而,deltaPosition
给出了上次 update 与当前位置之间的差异。因此,假设您的应用 运行 每秒 60 帧,它将 return 每 1/60 秒的距离。当然,这将是一个接近于零的数字,我相信这就是为什么它看起来总是 return 的起始位置
https://docs.unity3d.com/ScriptReference/Touch-deltaPosition.html
您必须按照与使用鼠标方法进行操作类似的方式进行操作。在 Touchphase.Began
上设置变量 touchStart
并将其与 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++;
}
}
虽然我还没有测试过,如果我错了请纠正我!