C# 统一刷卡功能问题
我在为游戏创建滑动控件时遇到了一些问题。我正在尝试重新创建游戏Subway Surfers的控件,在触控移动阶段而不是触控结束阶段检测到滑动。现在它大部分时间都能工作,但偶尔(通常)垂直滑动会被处理为水平滑动。 我已经提出了下面的代码,提前感谢任何帮助C# 统一刷卡功能问题,c#,unity3d,touch,swipe,C#,Unity3d,Touch,Swipe,我在为游戏创建滑动控件时遇到了一些问题。我正在尝试重新创建游戏Subway Surfers的控件,在触控移动阶段而不是触控结束阶段检测到滑动。现在它大部分时间都能工作,但偶尔(通常)垂直滑动会被处理为水平滑动。 我已经提出了下面的代码,提前感谢任何帮助 int swipeIndex = -1; //index of first detected swipe to prevent multiple swipes Vector2 startPos = Vector.zer
int swipeIndex = -1; //index of first detected swipe to prevent multiple swipes
Vector2 startPos = Vector.zero; //starting position of touch
//minSwipeDist is the distance in inches the player must swipe for a correct swipe
for (int i = 0; i < Input.touchCount; i++) {
Touch touch = Input.touches[i];
switch (touch.phase) {
case TouchPhase.Began:
if (swipeIndex == -1) {
swipeIndex = i;
startPos = touch.position;
}
break;
case TouchPhase.Moved:
if(i != swipeIndex)
break;
Vector2 direction = touch.position - startPos;
//vertical swipe
if (Mathf.Abs(direction.x) < Mathf.Abs(direction.y)) {
//swipe up
if ((touch.position.y - startPos.y) > minSwipeDist) {
//Process Up swipe
swipeIndex = -1;
}
//swipe down
else if ((touch.position.y - startPos.y) < -minSwipeDist) {
//Process down swipe
swipeIndex = -1;
}
}
//horizontal swipe
else {
//swipe left
if ((touch.position.x - startPos.x) < -minSwipeDist) {
//process left swipe
swipeIndex = -1;
}
//swipe right
else if ((touch.position.x - startPos.x) > minSwipeDist) {
//process right swipe
swipeIndex = -1;
}
}
break;
}
}
int swipeIndex=-1//第一次检测到的用于防止多次刷卡的刷卡索引
Vector2 startPos=Vector.zero//触摸起始位置
//MinswipDist是玩家必须刷卡才能正确刷卡的距离(以英寸为单位)
对于(int i=0;iMinswipDist){
//处理向上滑动
瑞士指数=-1;
}
//向下滑动
否则如果((touch.position.y-startPos.y)<-minswipdist){
//进程向下滑动
瑞士指数=-1;
}
}
//横扫
否则{
//左击
如果((touch.position.x-startPos.x)<-minswipdist){
//进程左扫
瑞士指数=-1;
}
//向右滑动
else if((touch.position.x-startPos.x)>minswipdist){
//进程右击
瑞士指数=-1;
}
}
打破
}
}
我认为您应该将代码从TouchPhase移动到TouchPhase。已结束。
基本上,当TouchPhase.开始时,您会获得接触点,然后在TouchPhase.中不断更新第二个点。移动,然后在TouchPhase.End中处理这两个点