我正在尝试制作2D无限亚军游戏,但滑动操作无法正常进行。 滑动时会有明显的延迟,只有在我停止触摸并抬起手指后才发生滑动。
如何修改脚本以使滑动更负责,并在手指位于屏幕上时滑动?
我希望有人可以提供帮助。
void Update()
{
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
startSwipePoz = Input.GetTouch(0).position;
}
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
endSwipePoz = Input.GetTouch(0).position;
if (endSwipePoz.x < startSwipePoz.x && transform.position.x > -1.83f)
{
StartCoroutine(Move("left"));
}
if (endSwipePoz.x > startSwipePoz.x && transform.position.x < 1.83f)
{
StartCoroutine(Move("right"));
}
}
}
private IEnumerator Move(string flySide)
{
switch(flySide)
{
case "left":
flyTime = 0f;
startPoz = transform.position;
endPoz = new Vector3(startPoz.x - 1.83f,transform.position.y,transform.position.z);
while (flyTime < flyDuration)
{
flyTime += Time.deltaTime;
transform.position = Vector2.Lerp(startPoz,endPoz,flyTime / flyDuration);
yield return null;
}
break;
case "right":
flyTime = 0f;
startPoz = transform.position;
endPoz = new Vector3(startPoz.x + 1.83f,flyTime / flyDuration);
yield return null;
}
break;
}
}