【发布时间】:2017-06-27 14:24:52
【问题描述】:
我正在实现特定类型的触摸控制器。 玩家需要将手指放在屏幕上才能移动。 在不抬起手指的情况下,玩家可以在移动的同时向不同方向滑动以改变方向。 一旦手指抬起,播放器就会停止移动。
很难隔离特定的滑动(即在屏幕上绘制的线条)而忽略任何其他不打算绘制线条的动作。 例如,当玩家的手指“静止”时,手指的轻微移动会破坏我的算法。
我考虑了不同的方法,例如存储最后几次触摸并评估它们以确定是否有滑动,但无法正确实施。
这是我迄今为止尝试过的。大多数情况下它运行良好,但玩家经常做不稳定的运动并且完全与我的预期相反。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchInputHandler : AbstractInputHandler {
private const int SWIPE_MIN_DISTANCE = 35;
private Vector2 touchStartPoint;
private Vector2 touchMovePoint;
void Update () {
Touch[] touches = Input.touches;
if (touches.Length == 1) {
Touch firstTouch = touches [0];
if (firstTouch.phase == TouchPhase.Began) {
this.touchStartPoint = firstTouch.position;
fireNextDirectionChanged (currentDirection);
} else if (firstTouch.phase == TouchPhase.Moved) {
this.touchMovePoint = firstTouch.position;
if (Vector2.Distance(touchStartPoint, touchMovePoint) > SWIPE_MIN_DISTANCE) {
detectSwipeDirection ();
}
} else if (firstTouch.phase == TouchPhase.Stationary) {
touchStartPoint.x = touchMovePoint.x;
touchStartPoint.y = touchMovePoint.y;
} else if (firstTouch.phase == TouchPhase.Ended) {
fireNextDirectionChanged (Constants.Direction.NONE);
}
}
}
private void detectSwipeDirection() {
float xDiff = touchMovePoint.x - touchStartPoint.x;
float yDiff = touchMovePoint.y - touchStartPoint.y;
Constants.Direction nextDirection;
bool yGreater = Mathf.Abs(yDiff) >= Mathf.Abs(xDiff);
if (yGreater) {
// direction is up or down
nextDirection = yDiff < 0 ? Constants.Direction.DOWN : Constants.Direction.UP;
} else {
// direction is left or right
nextDirection = xDiff < 0 ? Constants.Direction.LEFT : Constants.Direction.RIGHT;
}
if (nextDirection != this.currentDirection)
{
fireNextDirectionChanged (nextDirection);
this.currentDirection = nextDirection;
}
}
}
【问题讨论】:
-
@Programmer,这个问题怎么重复?你读过这个问题吗
-
两个问题都说“检测滑动”。至于“不举手”,请检查答案。它具有设置为 false 的
detectSwipeOnlyAfterRelease变量。请告诉我,什么不会使这个重复? -
@Programmer 是的,你是对的,我在那里阅读了答案,根据我的需要对其进行了一些修改并解决了我的问题。我是发布我的解决方案并接受它还是将其作为重复项关闭?
-
它已经关闭,但您可以提出自己的答案。还链接到您修改的原始代码。