难道 Unit 和 Android 不能精确跟随手指吗?
Is Unit and Android unable to precisely follow a finger?
我正在使用 Unity 创建一个 Android 游戏,我试图让一个对象跟随我的手指。这是我正在使用的代码。
for (int i = 0; i < Input.touchCount; i++)
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.touches[i].position), Vector2.zero);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(Input.touches[i].position);
if (Input.GetTouch(i).phase == TouchPhase.Moved && hit.collider != null)
{
transform.position = new Vector2(touchPosition.x, touchPosition.y + sizeY);
}
}
我将对撞机放在对象下方,并将对象的大小添加到 Y,因此它出现在我的手指上方。
问题是,如果我稍微快点移动手指,我就会失去留在后面的物体。由于物体滞后,如果我更快地移动手指,我最终会离开碰撞器区域,物体就会停止移动。
我发现唯一可行的方法是摆脱 Raycast 的东西,让对象跟随屏幕上任何地方的触摸(我不喜欢那样)。
我只是想知道这是否是我能从 Unity 和 Android 获得的最好的结果,或者是否有办法使对象以一个接一个的方式精确地跟随手指移动,没有滞后。
我正在 Samsung Galaxy S8 上仅使用该对象进行测试,没有其他任何东西。
遇到这个问题的不止你一个人。 Unity 的输入 API 是基于帧率的,可能跟不上屏幕上触摸移动的速度。如果您正在处理需要基于屏幕上的触摸的快速响应的事情,请使用新的 Unity InputSystem。此 API 仍处于实验模式。
其Touchscreen
class可用于访问屏幕上的触摸。您当前的代码应转换为如下所示:
public int touchIndex = 0;
void Update()
{
Touchscreen touchscreen = UnityEngine.Experimental.Input.Touchscreen.current;
if (touchscreen.allTouchControls[touchIndex].ReadValue().phase != PointerPhase.None &&
touchscreen.allTouchControls[touchIndex].ReadValue().phase == PointerPhase.Moved)
{
Vector2 touchLocation = touchscreen.allTouchControls[touchIndex].ReadValue().position;
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touchLocation), Vector2.zero);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touchLocation);
transform.position = new Vector2(touchPosition.x, touchPosition.y + sizeY);
}
}
我正在使用 Unity 创建一个 Android 游戏,我试图让一个对象跟随我的手指。这是我正在使用的代码。
for (int i = 0; i < Input.touchCount; i++)
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.touches[i].position), Vector2.zero);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(Input.touches[i].position);
if (Input.GetTouch(i).phase == TouchPhase.Moved && hit.collider != null)
{
transform.position = new Vector2(touchPosition.x, touchPosition.y + sizeY);
}
}
我将对撞机放在对象下方,并将对象的大小添加到 Y,因此它出现在我的手指上方。 问题是,如果我稍微快点移动手指,我就会失去留在后面的物体。由于物体滞后,如果我更快地移动手指,我最终会离开碰撞器区域,物体就会停止移动。 我发现唯一可行的方法是摆脱 Raycast 的东西,让对象跟随屏幕上任何地方的触摸(我不喜欢那样)。
我只是想知道这是否是我能从 Unity 和 Android 获得的最好的结果,或者是否有办法使对象以一个接一个的方式精确地跟随手指移动,没有滞后。 我正在 Samsung Galaxy S8 上仅使用该对象进行测试,没有其他任何东西。
遇到这个问题的不止你一个人。 Unity 的输入 API 是基于帧率的,可能跟不上屏幕上触摸移动的速度。如果您正在处理需要基于屏幕上的触摸的快速响应的事情,请使用新的 Unity InputSystem。此 API 仍处于实验模式。
其Touchscreen
class可用于访问屏幕上的触摸。您当前的代码应转换为如下所示:
public int touchIndex = 0;
void Update()
{
Touchscreen touchscreen = UnityEngine.Experimental.Input.Touchscreen.current;
if (touchscreen.allTouchControls[touchIndex].ReadValue().phase != PointerPhase.None &&
touchscreen.allTouchControls[touchIndex].ReadValue().phase == PointerPhase.Moved)
{
Vector2 touchLocation = touchscreen.allTouchControls[touchIndex].ReadValue().position;
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touchLocation), Vector2.zero);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touchLocation);
transform.position = new Vector2(touchPosition.x, touchPosition.y + sizeY);
}
}