AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > Unity3D

【Unity C#编程】跑动游戏(3):跳跃与碰撞

51自学网 2014-05-24 http://www.51zixue.net

生成平台

针对这个游戏增加的平台基本是和生成Skyline是相同的,仅有少数的差异。平台的高度需要随机改变(游戏开始时),这就是两者之间的区别。同时,我们也要限制平台高度,以确保Skyline的视角。如果平台高度超出了范围,我们给它弹回范围内。

在Project视图中创建一个新的文件命名为Platform,在文件夹中创建一个新的C#脚本名为PlatformManager,将SkylineManager的代码复制过来,然后根据我们的需要再修改代码。

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
usingUnityEngine;
 
usingSystem.Collections.Generic;
 
 
 
publicclassPlatformManager: MonoBehaviour {
 
 
 
publicTransform prefab;
 
publicintnumberOfObjects;
 
publicfloatrecycleOffset;
 
publicVector3 startPosition;
 
publicVector3 minSize, maxSize, minGap, maxGap;
 
publicfloatminY, maxY;
 
 
 
privateVector3 nextPosition;
 
privateQueue<Transform> objectQueue;
 
 
 
voidStart () {
 
objectQueue =newQueue<Transform>(numberOfObjects);
 
for(inti = 0; i < numberOfObjects; i++){
 
objectQueue.Enqueue((Transform)Instantiate(prefab));
 
}
 
nextPosition = startPosition;
 
for(inti = 0; i < numberOfObjects; i++){
 
Recycle();
 
}
 
}
 
 
 
voidUpdate () {
 
if(objectQueue.Peek().localPosition.x + recycleOffset <Runner.distanceTraveled){
 
Recycle();
 
}
 
}
 
 
 
privatevoidRecycle () {
 
Vector3 scale =newVector3(
 
Random.Range(minSize.x, maxSize.x),
 
Random.Range(minSize.y, maxSize.y),
 
Random.Range(minSize.z, maxSize.z));
 
 
 
Vector3 position = nextPosition;
 
position.x += scale.x * 0.5f;
 
position.y += scale.y * 0.5f;
 
 
 
Transform o = objectQueue.Dequeue();
 
o.localScale = scale;
 
o.localPosition = position;
 
objectQueue.Enqueue(o);
 
 
 
nextPosition +=newVector3(
 
Random.Range(minGap.x, maxGap.x) + scale.x,
 
Random.Range(minGap.y, maxGap.y),
 
Random.Range(minGap.z, maxGap.z));
 
 
 
if(nextPosition.y < minY){
 
nextPosition.y = minY + maxGap.y;
 
}
 
elseif(nextPosition.y > maxY){
 
nextPosition.y = maxY - maxGap.y;
 
}
 
}
 
}

现在,让我们在platforms里制作一个prefab,以及它所需要的材料。你可以直接复制Skyline中任意一个材料,然后修改它的颜色为(255, 60, 255),并在Platform Regular Mat中调用它。再创建一个新的cube,把刚才的材料分配给cube,将其命名为platform。把两个新建文件全部放进Platform文件夹中。

Platform prefab

创建一个新的空object命名为Platform Manager,让他成为Managers的一个子集,再通过draggin脚本赋予它一个Platform Manager组件,配置Platform Prefab的Prefab字段。设置Number Of Objects为6,Recycle Offset为20,Start Position为(0,0,0),Min Size为(5,1,1),Max Size为(10,1,1)。然后再配置字段的Min Gap、Max Gap、Min Y和Max Y为(2,-1.5,0),(4,1.5,0),-5和10。

platform manager

managers

Platform manager

跳跃与掉落

现在我们有了平台,是时候该增加我们的Runner了。我将使用Unity物理引擎对平台开发跳跃、掉落和碰撞功能。因此,要给Runner添加一个Rigidbody组件。我们不希望它在运行中发生旋转或跑得无影无踪,所以我们要固定Z位置和所有转轴。

由于移动是以滑行方式跳跃平台,所以要创建一个物理材料,将其摩擦力全部取消。设置所有的字段为0,这样将可以在任何设定下滑行。

命名新的物理材料为Runner PMat,将其放于Runner文件夹中,再分配Runner的Box Collider的Material值。

重新定为Runner为(0,2,0),游戏开始后,Runner将会从屏幕上方掉落到平台上。你可以尝试播放一下,看看会发生什么!

runner

material

game

Runner with physics

你应该已经看到了,Runner掉落在平台上,然后向右移动,通过跳跃可以移动到其他平台上。但是,当碰撞到下一个平台边沿上就会发生一个怪异的行为——加速,这是因为我们一直在Update属性里不断变换它的位置,致使物理引擎的效果被一个施加的力所替代。

我使用了两个Unity碰撞事件属性——OnCollisionEnter和OnCollisionExit,检测碰撞或离开平台的状态。只要我们接触到平台,就会加快移动的速度。

现在让我们在编辑器中配置加速度吧。

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
usingUnityEngine;
 
 
 
publicclassRunner: MonoBehaviour {
 
 
 
publicstaticfloatdistanceTraveled;
 
 
 
publicfloatacceleration;
 
 
 
privatebooltouchingPlatform;
 
 
 
voidUpdate () {
 
distanceTraveled = transform.localPosition.x;
 
}
 
 
 
voidFixedUpdate () {
 
if(touchingPlatform){
 
rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
 
}
 
}
 
 
 
voidOnCollisionEnter () {
 
touchingPlatform =true;
 
}
 
 
 
voidOnCollisionExit () {
 
touchingPlatform =false;
 
}
 
}

还有一件事,我们需要提前做。复制Runner PMat,重命名为Platform Tegular PMat,将其移动到Platform文件夹中。设置两个摩擦字段为0.05并拖拽Platform Prefab的物理材料里。

虽然我们对平台添加了一点的摩擦力,但Runner在移动过程中已经有足够的加速度了。

acceleration

platform

physic material

Acceleration and regular platform

为了让Runner能跳起来,我们需要检测玩家的输入。Unity跳跃动作的默认设置是在Edit/Project Settings/Input中。我们可以在 Alt Positive Button中配置X键为跳跃按钮。

Jump input configuration

现在添加一个Runner变量,用来控制它跳跃的速度。我们用一个载体来替换浮点数,因此我们可以设置垂直和水平两个组件,在编辑中设置相应的字段为(1,7,0)。

按下跳跃按钮时,我们只希望Runner能从平台上跳起来。让我们看看添加的Updata代码吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
publicVector3 jumpVelocity;
 
 
 
voidUpdate () {
 
if(touchingPlatform && Input.GetButtonDown("Jump")){
 
rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
 
}
 
distanceTraveled = transform.localPosition.x;
 
}

settings

game

Runner jumping

现在我们可以跳起来了。但是,如果Runner碰撞到平台的侧边,在接触平台的瞬间,我们还是可以执行多次跳跃,这会使Runner在两个平台中来回上下跳跃。为了防止这种情况,我们决定一旦跳起来碰撞到平台侧面,就只能再跳跃一次。

1
2
3
4
5
6
7
8
9
10
11
12
13
voidUpdate () {
 
if(touchingPlatform && Input.GetButtonDown("Jump")){
 
rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
 
touchingPlatform =false;
 
}
 
distanceTraveled = transform.localPosition.x;
 
}


原文:http://catlikecoding.com/unity/tutorials/runner/


建议使用电驴(eMule)下载分享的资源。

说明
:本教程来源互联网或网友分享或出版商宣传分享,仅为学习研究或媒体推广,51zixue.net不保证资料的完整性。
 
上一篇:【Unity C#编程】跑动游戏(4):平台与游戏事件  下一篇:【Unity C#编程】跑动游戏(2):生成Skyline