您面临的问题是因为您在TouchPhase.Ended中进行了检查。您想要做的是在TouchPhase.Moved中执行检查,并使用较小的值更改(如果代码不起作用,则使用80在Ended中,尝试使用10等)。
Unity关于TouchPhase的文档
http://docs.unity3d.com/ScriptReference/TouchPhase.html
foreach(Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fingerStart = touch.position;
fingerEnd = touch.position;
}
if (touch.phase == TouchPhase.Moved )
{
fingerEnd = touch.position;
if((fingerStart.x - fingerEnd.x) > 80 ||
(fingerStart.x - fingerEnd.x) < -80)
{
leftRight ++;
}
else if((fingerStart.y - fingerEnd.y) < -80 ||
(fingerStart.y - fingerEnd.y) > 80)
{
upDown ++;
}
if(leftRight >= 3){
leftRight = 0;
}
if(upDown >= 4){
upDown = 0;
}
fingerStart = touch.position;
}
if(touch.phase == TouchPhase.Ended)
{
leftRight = 0;
upDown = 0;
fingerStart = Vector2.zero;
fingerEnd = Vector2.zero;
}
如果您想明确地检查模式(即从左到右,然后从右到左),而不仅仅是像您现有的代码那样检查它是否是一些横向/纵向移动,请尝试以下代码。只需记住包括System.Collections.Generic和System.Linq命名空间。
private Vector2 fingerStart;
private Vector2 fingerEnd;
public enum Movement
{
Left,
Right,
Up,
Down
};
public List<Movement> movements = new List<Movement>();
void Update () {
foreach(Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began) {
fingerStart = touch.position;
fingerEnd = touch.position;
}
if(touch.phase == TouchPhase.Moved) {
fingerEnd = touch.position;
if(Mathf.Abs(fingerStart.x - fingerEnd.x) > Mathf.Abs(fingerStart.y - fingerEnd.y)) {
if((fingerEnd.x - fingerStart.x) > 0)
movements.Add(Movement.Right);
else
movements.Add(Movement.Left);
}
else {
if((fingerEnd.y - fingerStart.y) > 0)
movements.Add(Movement.Up);
else
movements.Add(Movement.Down);
}
fingerStart = touch.position;
Debug.Log (CheckForPatternMove(0, 3, new List<Movement>() { Movement.Left, Movement.Right, Movement.Left } ));
}
if(touch.phase == TouchPhase.Ended)
{
fingerStart = Vector2.zero;
fingerEnd = Vector2.zero;
movements.Clear();
}
}
}
private bool CheckForPatternMove (int startIndex, int lengthOfPattern, List<Movement> movementToCheck) {
if(lengthOfPattern > movements.Count)
return false;
if(startIndex + lengthOfPattern > movements.Count)
return false;
List<Movement> tMovements = new List<Movement>();
for(int i = startIndex; i < startIndex + lengthOfPattern; i++)
tMovements.Add(movements[i]);
return tMovements.SequenceEqual(movementToCheck);
}
编辑 添加了更多代码作为示例
movements = new List<Movement>()
{
Movement.Left,
Movement.Right,
Movement.Left
};
Debug.Log (CheckForPatternMove(0, 3, new List<Movement>() { Movement.Left, Movement.Right, Movement.Left } ));
这是我的更新函数的样子。请注意在Input.touch上使用GetMouseButton的用法。
void Update () {
if (Input.GetMouseButtonDown(0)) {
fingerStart = Input.mousePosition;
fingerEnd = Input.mousePosition;
}
if(Input.GetMouseButton(0)) {
fingerEnd = Input.mousePosition;
if(Mathf.Abs(fingerEnd.x - fingerStart.x) > tolerance ||
Mathf.Abs(fingerEnd.y - fingerStart.y) > tolerance) {
if(Mathf.Abs(fingerStart.x - fingerEnd.x) > Mathf.Abs(fingerStart.y - fingerEnd.y)) {
if((fingerEnd.x - fingerStart.x) > 0)
movements.Add(Movement.Right);
else
movements.Add(Movement.Left);
}
else {
if((fingerEnd.y - fingerStart.y) > 0)
movements.Add(Movement.Up);
else
movements.Add(Movement.Down);
}
fingerStart = fingerEnd;
Debug.Log (CheckForPatternMove(0, 3, new List<Movement>() { Movement.Left, Movement.Right, Movement.Left } ));
}
}
if(Input.GetMouseButtonUp(0)) {
fingerStart = Vector2.zero;
fingerEnd = Vector2.zero;
movements.Clear();
}
}