查看: 2607|回复: 3
打印 上一主题 下一主题

FPS游戏教程3 (中文版)

[复制链接]

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-8-7 10:26:44 |只看该作者 |倒序浏览


Fps游戏教程3
Part 3: Advanced FPS 第三部分 高级FPS

This advancedlevel tutorial extends upon the previous FPS tutorials by introducing game elements such as waypoints, enemy AI, ragdolls and animation.

这个高级教程是原先FPS教程的延伸,介绍游戏元素,例如路径点、敌人AI,布偶和动画。
Prerequisites 先决条件

This tutorial assumes that you are familiar with the Unity interface and basic scripting

concepts. Additionally, you should already be familiar with the concepts discussed in

Part 1 and 2 of the FPS tutorial series.

这个指南已经默认了你已经熟悉了Unity界面和基本的脚本概念。你已经熟悉了第一部分和第二部分的FPS概念的讨论。

Setting up设置

We’re going to build upon the previous FPS tutorial, so we’ll begin by opening that:

我们将延伸我们的FPS教程,让我们开始吧:

Open the previous project (FPS Tutorial 2).

打开原先的项目(FPS 教程 2)

Waypoints 路径点

This section will introduce waypoints to our game, these are used to inform the robots

of the path that they can walk around. Let’s add three waypoints to our game:

这一节将介绍路径点到我们游戏中,即告知机器人它们能够在周围巡逻的路径,让我们增加三个路径点到我们游戏中:

Create an empty game object and rename this to Waypoint. Make sure the game object is placed approximately one meter above the ground level.

建立一个空的游戏对象并且重命名它为路径点(Waypoint),确保游戏对象被定位在地面上大约一米左右。

Add AutoWayPoint script to Waypoint. Notice how the empty game object now displays a W in the Scene View.

给路径点增加一个自动路径点(AutoWayPoint)脚本,注意怎么在场景视窗中显示空游戏对象(按W键)

Duplicate the WayPoint game object twice and arrange the waypoints in a triangle

shape (position quite far apart).

重复制作两个路径点游戏对象并且把它们排成一个三角形(距离远一些)
Check that the waypoints are visible to each other by firstly clicking on any waypoint then selecting Update Waypoint from the context menu of the AutoWayPoint script (the rightmost button on the component in the Inspector View). A green line will be drawn between visible waypoints, a red line for those which don’t have a clear line of sight.

检查一下这些路径点应该是彼此可见的,首先单击任一个路径点然后在自动路径点(AutoWayPoint)脚本的关联菜单上选择更新路径点(在检查视窗的组件上的最右边按钮),一个绿色的线将在可见的路径点之间画出来,一个红色的线表示视野中没有清晰的线。

We have now described the path that an enemy can walk around, now let’s add an opponent to the scene.

我们现在说明的是一个敌人的巡逻路线,现在让我们在场景中增加一个敌人。

Robot AI 机器人人工智能

This section will add an enemy robot to our scene.

这一节将增加一个敌人在我们的场景中。

Select Robot Artwork/robot and drag it into the Scene View making sure all of the robot is above the ground.

在Artwork/robot目录下选择机器人(Robot)并且拖拽它到场景中,确保所有的而机器人都在地面之上。

Let’s give the robot some behavior.

让我们给出机器人的一些行为。

Add WeaponsScripts/AI to robot. In the AI script section or robot, assign the FPS

controller as the target (so the robot knows who to hunt down).

增加一个WeaponsScripts/AI脚本给机器人。在AI脚本部分或机器人,都要将FPS控制器作为目标(所以机器人才能知道猎捕谁)

Add the AIAnimation script to the robot. This controls the animation of our robot (when to***n, when to aim etc). This communicates with the AI script to find out what the AI script is currently doing, e.g. are we***nning, shooting etc.It then crossfades the animations to provide a smooth transition.

增加一个AIAnimation脚本给机器人,这些会控制我们机器人的动画(什么时候跑,什么时候瞄准等等)。与人工智能进行交流,找出AI脚本当前应该做什么,例如,我们正在跑,射击等。然后他排演一个动画以提供一个平滑过渡。

Now we need to make the robot controller object a little larger so that he doesn’t intersect with the ground. This is because the character controller which is used to prevent the enemy from moving through walls is using a capsule to represent the AI. We need to make this capsule a little larger to match the actual graphics, this way he will not intersect with the ground anymore.

现在我们需要建造一个机器人控制器对象,稍大一点,以便它不会和地面相交。这时因为胶囊代表的人工智能使用的特性控制器(character controller)会阻止敌人穿墙移动。我们需要制造稍大一点的胶囊以匹配实际的图像,这样他不会再与地面相交。

Select the robot, then in the Inspector View modify the height and radius values

of the Character Controller component so that it encloses the robot. Press play

to make sure it works correctly.

选择机器人,然后再检查视窗中编辑特性控制器组件的高度和半径值以便它能包住机器人。按运行按钮确保它工作正确。

Try making the height and radius values smaller and larger to see the difference.

试着将高度和半径值改的稍大一些看看有什么不同。
Now we need to give the robot the ability to fire his gun. The robot is continuously moving and animating. When he shoots his gun we play an animation on the hands and the graphical gun then spawn the rocket from transform relative to the robot. This transform does not animate, it is simply placed approximately at the point where the gun will be when the shoot animation fires the rocket.

现在我们需要给机器人的枪开火的能力。该机器人在不断移动和动画(用动画表现其反应),当拍摄它手上的枪时我们播放一个动画和枪的图形然后相对于机器人变形衍生出火箭。这个transform不能做成动画,它很简单地大约放置在枪将发射火箭弹的动画的那个点。

Create an empty game object and make it a child of the robot (use the hierarchy view).

建立一个空的游戏对象并且确保它是机器人对象的子对象(使用层次视窗)

Rename the game object gun_spawn.

重命名游戏对象为gun_spawn

Now we need to place it properly. In the transform inspector select Reset from the context menu, then move it forward in the z-axis.

现在我们需要配置它的属性。在关联菜单上的transform检查器中选择Reset,然后沿Z轴移动它。

Add the rocket launcher script to the gun_spawn game object. Assign the rocket prefab to the projectile variable.

增加rocket launcher脚本到gun_spawn游戏对象上,分配火箭预制物体到projectile变量上。

Make a prefab of this robot, call it Robot. This will allow you to create more enemies easily.

制造一个机器人预制物体,调用它,这将允许你建立更多的敌人。

Play the game and check that the robot fires at you.

运行游戏并且检查那个朝你开火的机器人。

Although you can shoot the robot, it is not configured to take damage, we’ll remedy

this in the next section.

尽管你能朝机器人射击,但它没有配置获得伤害,在下一节中我们将补救这些。

Robot Damage 机器人伤害

Attach the CharacterDamage script to the Robot prefab.

附着CharacterDamage脚本到机器人预制物体上。

Play the game, shoot the robot with the machine gun and he should disappear.

运行游戏,用机枪朝机器人射击并且它会消失。

Ragdolls 布偶

Ragdolls emulate a natural skeletal bone s***cture, this will allow our robot to fall naturally when killed. This section will show how to setup a ragdoll.

布偶模仿一个天然的骨骼结构,这些将允许我们的机器人在被杀死时自然跌倒,这一节将显示如何设置一个布偶。

Firstly, create a new scene (File >New Scene). We’re going to setup our ragdoll in here. Save your current scene if necessary.

首先,建立一个新的场景(File >New Scene)。我们将在这装配我们的布偶,如果需要保存你当前的场景。

Create a cube (resize if necessary), this will be used as a platform for our robot. Drag in the robot to the Scene View so that it is positioned above the cube.

建立一个cube(如果需要就重定义一下尺寸),这将被用作我们的机器人的平台,拖拽机器人到场景视窗中以便它被定位到cube之上。

Remove the animation component. This is important otherwise the animations will interfere with the physics.

删除动画组件,这一点很重要,否则动画在物理上将被干扰。

Now use the ragdoll wizard: Game object >Create Other >Ragdoll.

现在使用布偶向导:Game object >Create Other >Ragdoll

A dialog box appears, now we have to assign all the bones to the correct slots in the wizard. We assign the bones by dragging the values from the Robot in the Hierarchy View onto the correct place in the dialog box. Expand the Robot gameobject in the Hierarchy View if necessary to reveal this.

出现一个对话框,现在我们必须在向导的正确插槽中指定所有的骨头,我们通过拖拽层次面板上的值分配机器人的骨头到对话框中正确的地方,如果需要在层次视窗中扩大机器人游戏对象以显露出它。

Firstly, rootHandle (Hierarchy View) maps onto root (in the dialog box). Drag rootHandle onto the placeholder beside root in the dialog box.

首先,rootHandle(层次视窗)映射到root(在对话框中),拖拽rootHandle到对话框的root旁边的定位手柄处。

Set Total Mass to 4

设置总质量为4

Legs 腿

Assign upleg_L to Left Hip.

分配左大腿(upleg_L)到Left Hip。

Assign lowleg_L goes to Left Knee.

分配左小腿(lowleg_L)到Left Knee

Assign heel_L goes to Left Foot.

分配左脚踝(heel_L)到Left Foot

Repeat for the right leg.

重复分配右腿

Upper body / Arms 上半身和胳膊

Assign upArm_L goes to Left Arm.

分配左手(upArm_L)到Left Arm

Assign elbow_L goes to Left Elbow.

分配左肘(elbow_L)到Left Elbow

Repeat for the right arm.

重复分配右手

Assign spine 3 goes to Middle spine.

分配脊柱3到中间脊柱。

Finally, head goes to Head.

最后头部到头部

Hit create then press play, the robot should fall, however the gun won’t.

单击建立并运行游戏,机器人将会倒下,然而枪没有这样做。

To make the gun fall:

制造枪落下:

Select the gun in the Hierarchy View.

在层次视窗中选择枪

Add a rigidbody component.

给它增加一个刚体组件。

Add a box collider. Adjust the size of the box collider to fit the gun, you’ll need

to adjust the center position also.

增加一个盒子碰撞器,调整盒子碰撞器的尺寸以适应枪的大小,你也需要调整位置中心。
Now we put the fully rigged robot into a prefab. To create the prefab:

现在我们把这个全操纵机器人放到预制物体中,建立这个预制物体:

Assets >Create >refab.

Rename it to RobotRagdoll.

重命名它为机器布偶

Drag the robot (root level) from the hierarchy view into the prefab. This will make sure the gun is attached also.

从层次视窗中拖拽机器人(根层)到预制物体上,这将确保枪也附带着放进去。

Now when the robot is killed, we delete the old robot and instantiate the new ragdoll robot, this allows our robot to fall to the ground naturally.

现在当机器人被杀死时,买我们将删除老的机器人并且初始化一个新的玩偶机器人,使我们的机器人很自然的倒在地面上。

Using the Ragdoll 使用布偶

Open the original scene (no need to save the current scene), select the robot from the Hierarchy View and in the Character Damage inspector, drag in the RobotRagdoll prefab to the Dead Replacement.

打开一个原始场景(不需要保存当前场景),从层次视窗中选择机器人并且在伤害特性检查器中,拖拽一个机器人玩偶预制物体到死亡替代物上。

Play the game, shoot the robot and it should now fall down when shot.

运行游戏,朝机器人射击当射中时可以看到机器人会倒下

You may want to tweak the mass of the robot to improve the look & feel of the ragdoll. You can do this in the ragdoll wizard, by changing the mass property. This is especially important in explosion forces where the robot is thrown into the air.

你可能想调整一下机器人的质量以改善机器玩偶看到的感觉。你能在机器玩偶向导中通过改变质量属性做到。当机器人手爆炸影响被抛到空气中时这点特别重要。

Sound 声音

This section will add sound effects to our game.

这一节将给我们的游戏增加声音效果。

Machine gun 机枪

Drag machineGunSingleShotLoopable onto the MachineGun gameobject.

拖拽machineGunSingleShotLoopable声音文件到机枪游戏对象上

Turn off Play on Awake.

关闭Play on Awake

Rocket Launcher 火箭发射器

We want to attach the sound source of the rocket to the rocket so that the audio levels reduces as it flies away.

我们想附加一个火箭弹声音源到火箭弹上,以便随着它的飞行,声音的大小也在减弱。

Select the Rocket prefab.

选择火箭弹预制物体

Add an Audio Source component to the prefab.

增加一个声音源组件到预制物体上

Drag the RocketLauncherFire audio source to the Audio Clip property of the Audio Source.

拖拽RocketLauncherFire声音文件到声音源的Audio Clip属性上。

Adjust the Rolloff Factor so that the sound fades out faster (0.5 might be ok).

调整Rolloff Factor以便声音衰减的更快(可能0.5秒吧)

GUI 图形用户界面

The GUI (Graphical User Interface) is responsible for giving feedback to the player on the current game status. Typical GUI elements include; number of bullets left, health, mission objectives etc.

在当前的游戏状态下GUI(用户图形界面)承担着给游戏者一个反馈的任务。典型的GUI元素包括:弹夹中的弹药数目,生命值,目标使命等等。

In this game we have three main GUI elements.

在这个游戏中,我们有三个主要的GUI元素。

Overlay 覆盖

The overlay sets up the different zones of the GUI (health, ammo etc).

Overlay设置不同的GUI区域(生命值,弹药等等)

Select the GUI Overlay texture in GUI/GUIOverlay.

在GUI/GUIOverlay中选择GUI 覆盖材质

Select GameObject >Create Other >GUITexture, this will place the texture in the centre of the Game View.

选择GameObject >Create Other >GUITexture,将会替代游戏视窗中心的材质

We now need to position the overlay in the correct place:

我们现在需要定位overlay到正确的位置上:

Modify the transform Position to 0, 0, 0.

编辑坐标值为0,0,0

Modify the Pixel Inset to 0, 0, 256, 128 (in order X, Y, Width, Height).

编辑Pixel Inset值为0, 0, 256, 128(按次序为X, Y, Width, Height)

Health 健康

Select GUI/healthBar in the Project View.

在项目视窗中选择GUI/healthBar。

Select GameObject >Create Other >GUI Texture.

选择GameObject >Create Other >GUI Texture.

Set the transform Position to 0, 0, 0.

设置坐标值为0, 0, 0

The pixel inset values are 37, 83, 111, 29.

pixel inset值是37, 83, 111, 29

The healthBar texture is scaled in realtime to cover the correct amount of the rippled area beside the health icon.

意译:health 图标旁边的healthBar材质是用来显示生命值大小的,在实际使用中可能要求实时覆盖,设置显示的区域大小

Machine Gun 机枪

Select Game Object >Create Other >Text.

选择Game Object >Create Other >Text

Set transform Position to 0, 0, 0.

设置坐标为0, 0, 0

Set Pixel Offset to 188,109.

设置Pixel Offset为188,109

You can change the text if wanted (for now), a script will set text when the game is***n.

你也可以改变文本到你想要的,当游戏运行时一个脚本将用来设置文本

Rockets 火箭弹

Select GameObject>Create Other >GUI Texture.

选择GameObject>Create Other >GUI Texture

Set Texture to None.

设置材质为空

Drag Misc Scripts/DrawRockets.js onto the GUI Texture.

拖拽Misc Scripts/DrawRockets.js脚本到GUI材质上

Set Rocket Texture to GUIRocket21.

设置火箭弹材质为GUIRocket21

Set Pos Y to 68.

设置Pos Y为68

Finally, add the FPSPlayer script to the FPS controller, assign the GUIs to the properties in the FPSPlayer script section.

最后,增加FPSPlayer脚本到FPS控制器上,分配GUI到FPS脚本节的属性上

Finally... 最后

We’ve nearly finished our tutorial, but let’s add some additional sounds first.

我们几乎完成了我们的教程,但首先让我们增加一些附加的声音

Player Damage Sounds 玩家受伤的声音

To the FPS Player script of the FPS controller game object assign the following sounds:

moanOfPainSmall 小的受伤

moanOfPainBig 大的受伤

playerDie 玩家死亡

Walk sounds 巡逻声音

We can assign any number of walk sounds to our FPS controller (to avoid repetition), these are chosen randomly from a variablesized list.

我们能给我们FPS控制器分配一些走路的声音(为避免重复),这些在可变列表中被随机关闭。

Select FPS player. Expand the triangle for Walk Sounds and set Size to 5. Drag footstep15 from Sounds/ into each of the five elements.

选择FPS玩家,扩展走路声音的三角形并且设置Size=5,从Sounds/目录下拖拽footstep15文件5个元素中。

Add an Audio Source component to the FPS player (no need to do anything else).

增加一个声音源组件到FPS玩家(不需要做任何事情)
Lights for moving objects 移动物体的亮度

The scene in our game is lightmapped, however the characters and rigidbodies can not use this lightmap since they are moving. Thus, we need to setup some lights which only affect those objects and look similar to the lighting of the lightmap. For this purpose we use the lights culling mask.

我们场景中的游戏是被亮度贴图的,然而一旦他们移动,特性和刚体组件就不能使用这个亮度贴图,这样的话我们需要设置一些能影响这些对象的灯光并且使它们看起来被灯光贴图照亮一样。为了这个目的我们使用灯光剔除遮罩。

Create a directional light.

建立一个直射光源(模拟太阳光)

We’ll alter the light’s culling mask, this determines what the directional light is lighting.

我们更改灯光剔除遮罩,这确定了多少方向光线能被照射过来。

First goto Edit >roject Settings >Tags, click on User Layer 8 and type Lightmapped.

首先去Edit >roject Settings >Tags菜单,单击User Layer 8并且键入Lightmapped。

Now select the directional light again and turn off Lightmapped.

现在再次选择方向光线并且关闭灯光贴图。

Select mainLevelMesh, change the Layer property to Lightmapped. Apply this change to all child objects when prompted.

选择mainLevelMesh,改变层属性为灯光贴图,当出现提示时应用这个改变到所有的子对象上。

The end of the beginning... 这只是个开始的结束

This completes our FPS tutorial series. Many key lessons for game development have been presented and will apply to other styles of games: not only FPSstyle games. We hope that you have learned the fundamentals of game creation and are now motivated to design your own novel games with Unity.

这些完成了我们的FPS系列教程,游戏开发的很多关键课程会被提出并且将应用到其它类型的游戏中,不仅仅只是FPS类型的游戏。我们希望你已经学习了游戏创作的基础,并且现在可以用Unity有目的的设计你的新颖游戏。

Acknowledgments 感谢

Special thanks go to Joachim Ante (code) and Ethan Vosburgh (graphics) for their help

in the making of this tutorial.

特别感谢在写这个教程时Joachim Ante (code)和Ethan Vosburgh (graphics)他们的帮助。

The next tutorial in the FPS series will demonstrate advanced concepts such as

ragdoll character animation, advanced AI, and adding polish to our game.

The next tutorial in the FPS series will demonstrate advanced concepts such as

ragdoll character animation, advanced AI, and adding polish to our game.

接下来的系列教程中的将展示更先进的概念,例如:布偶角色动画,先进的人工智能,并且增加一个 polish到我们的游戏中
? 播放声音代码 一个能控制屏幕位置和大小的代码 ?

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-8-7 10:50:51 |只看该作者

    哎呀~各位别光看不回帖啊~  支持下啊!
回复

使用道具 举报

.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

活跃会员 优秀版主 荣誉管理 论坛元老

板凳
发表于 2012-11-8 21:13:30 |只看该作者
这是一个好帖子,大家快来围观!!
回复

使用道具 举报

1

主题

1

听众

720

积分

初级设计师

Rank: 3Rank: 3

纳金币
339
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2013-11-7 09:15:47 |只看该作者
支持一下,文字太多了,看的有点晕
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-6 13:18 , Processed in 0.200175 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部