地铁跑酷操控程序解析

http://www.unitymanual.com/thread-10353-1-1.html

1.用于记录每次操作的开始点和结束点、开始时间和结束时间 

1
2
3
4
5
6
7
8
9
public class Swipe 

    // Fields 
    public Vector3 end; 
    public float endTime; 
    public Vector3 start; 
    public float startTime; 
  
}



2.四个滑动方向的枚举 
1
2
3
4
5
6
7
8
9
public enum SwipeDir 
  

    Up, 
    Down, 
    Left, 
    Right, 
    None 
}



3.监听Touch输入,得到输入的Swipe 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public void HandleControls() 

    if (!this._paused && (Input.touchCount > 0)) 
    { 
        Touch touch = Input.touches[0]; 
        if (touch.phase == TouchPhase.Began) 
        { 
            this.currentSwipe = new Swipe(); 
            this.currentSwipe.start = (Vector3) touch.position; 
            this.currentSwipe.startTime = Time.time; 
        } 
        if ((((touch.phase == TouchPhase.Moved) || (touch.phase == TouchPhase.Ended)) || (touch.phase == TouchPhase.Canceled)) && (this.currentSwipe != null)) 
        { 
            this.currentSwipe.endTime = Time.time; 
            this.currentSwipe.end = (Vector3) touch.position; 
            SwipeDir swipeDir = this.AnalyzeSwipe(this.currentSwipe); 
            if (swipeDir != SwipeDir.None) 
            { 
                if (this.characterState != null) 
                { 
                    this.characterState.HandleSwipe(swipeDir); 
                } 
                this.currentSwipe = null; 
            } 
        } 
        if ((touch.phase == TouchPhase.Ended) && (this.currentSwipe != null)) 
        { 
            this.currentSwipe.endTime = Time.time; 
            this.currentSwipe.end = (Vector3) touch.position; 
            if ((this.AnalyzeSwipe(this.currentSwipe) == SwipeDir.None) && (this.characterState != null)) 
            { 
                this.HandleTap(); 
            } 
        } 
    } 
}



4.处理、分析得到是Swip;通过向量的点乘,得到沿上下左右四个方向上分量做多的那个方向,作为最终结果。 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private SwipeDir AnalyzeSwipe(Swipe swipe) 

    Vector3 b = Camera.main.ScreenToWorldPoint(new Vector3(swipe.start.x, swipe.start.y, 2f)); 
    if (Vector3.Distance(Camera.main.ScreenToWorldPoint(new Vector3(swipe.end.x, swipe.end.y, 2f)), b) < this.swipe.distanceMin) 
    { 
        return SwipeDir.None; 
    } 
    Vector3 lhs = swipe.end - swipe.start; 
    SwipeDir none = SwipeDir.None; 
    float num2 = 0f; 
    float num3 = Vector3.Dot(lhs, Vector3.up); 
    if (num3 > num2) 
    { 
        num2 = num3; 
        none = SwipeDir.Up; 
    } 
    num3 = Vector3.Dot(lhs, Vector3.down); 
    if (num3 > num2) 
    { 
        num2 = num3; 
        none = SwipeDir.Down; 
    } 
    num3 = Vector3.Dot(lhs, Vector3.left); 
    if (num3 > num2) 
    { 
        num2 = num3; 
        none = SwipeDir.Left; 
    } 
    num3 = Vector3.Dot(lhs, Vector3.right); 
    if (num3 > num2) 
    { 
        num2 = num3; 
        none = SwipeDir.Right; 
    } 
    return none; 
}

地铁跑酷的操控手感灰常好,多多学习,多多借鉴。 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值