FPS游戏教程2
Part 2: Enhancements 第二部分 增强
This intermediate-level tutorial extends upon the Basic FPS tutorial by introducing game elements such as multiple weapons, damage and enemies.
这个中级教程是FPS基本教程的扩展,介绍游戏元素例如多种武器、毁伤和敌人。
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 of the FPS tutorial series.
这个指南已经默认了你已经熟悉了Unity界面和基本的脚本概念。你已经熟悉了第一部分的FPS概念的讨论。
Before we begin -level setup
在我们开始层面设置之前
Download FPS_Tutorial.zip, unzip, and open the project folder in Unity. If you
have completed Part 1, then unzip the files to a new folder.
下载FPS_Tutorial.zip解压缩并且在U你体验中打开项目,如果你有已完成的第一部分,这时解压缩它们到一个新的目录。
Import the Standard Assets Unity Package.
导入Standard Assets标准资源包
Add the mainLevelMesh and FPS controller prefab to the scene.
增加mainLevelMeshFPS控制预制物体到场景中。
NOTE In this tutorial no new scripts need to created. We’ll be using the ones that
were downloaded from the Unity Package.
注意 在这个指南中没有新的脚本需要建立,我们将使用下载的Unity软件包中的一些东西。
Weapon switching 武器开关
Before we discuss how to create each individual weapon, we need to write some code to manage how the weapons are initialized and switched from one to another. Let’s look at the Javascript for PlayerWeapons.js:
在我们讨论如何建立每一个个体的武器之前,我们需要写一些代码以便管理这些武器怎么
被初始化并且能被另一个(武器)关闭,让我们看一下PlayerWeapons.js:这个脚本代码:
function Awake()
{
// Select the first weapon 选择第一个武器
SelectWeapon(0);
}
This function initializes weapon 0 as the default weapon.
这个函数初始化武器0 为缺省的武器。
function Update()
{
// Did the user press fire?用户按开火?
if (Input.GetButton ("Fire1"))
BroadcastMessage("Fire");
if (Input.GetKeyDown("1"))
{
SelectWeapon(0);
}
else if (Input.GetKeyDown("2"))
{
SelectWeapon(1);
}
}
This function detects keyboard input; the fire button, the 1 button for weapon 1 or the 2 button for weapon 2. The weapons will be children objects of the Main Camera.
function SelectWeapon(index : int)
{
for (var i=0;i<transform.childCount;i++)
{
// Activate the selected weapon激活选择的武器
if (i == index)
transform.GetChild(i).gameObject.SetActiveRecursively(true);
// Deactivate all other weapon失效所有其它的武器
else
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
这个函数检测键盘输入;开火按钮,“1”按钮是武器1,“2”按钮时武器2。武器将被作为主照相机的子对象。
This activates the corresponding weapon depending on keyboard input.
Let’s use the above code.
激活响应的武器依赖键盘输入,让我们看上面的代码
Create an empty game object called Weapons. Move this so that it is a child object
to Main Camera (inside FPS controller). Our weapons will be added as children
of this object.
建立一个空的叫“Weapons”的游戏对象,移动它以便将它作为主照相机的子对象(在FPS控制器内),我们的武器将被增加为它的子对象。
Assign the PlayerWeapons.js script to the Weapons game object under Main
Camera.
分配PlayerWeapons.js脚本到主照相机下的Weapons游戏对象上。
We’ll now create our first weapon.
我们将建立我们的第一个武器
Rocket Launcher 火箭发射器
This section will describe how to make a rocket launcher style weapon.
这一节将描述如何制造一个火箭发射器类型的武器
Launcher 发射器
The rocket launcher is responsible for instantiating a rocket and giving it an initial velocity. The rocket will be launched directly at wherever the user is pointing and will be destroyed whenever it collides with another collider.
这个火箭发射器将承担初始化一个火箭并且给它一个初始速度的任务。这个火箭将被发射到用户指定的任何地方并且当它另一个碰撞器发生碰撞时火箭随即被销毁。
Add an empty game object and name it RocketLauncher. Position the game object in the approximate position where the FPS Controller’s hands would be.
增加一个空的游戏对象并且命名它为RocketLauncher。定位游戏对象到FPS控制器的手的附近
Add the RocketLauncher as a child to the Weapons game object inside the Main Camera in the Hierarchy View. This allows us to shoot wherever the camera is pointing and also makes sure that the RocketLauncher game object follows the FPS Controller as it moves around (as Main Camera is a child of FPS Controller).
在层次面板视窗中的主照相机的Weapons游戏对象上增加RocketLauncher子对象。
Click on Objects/weapons/rocketLauncher in the Project window and make sure
that the FBXImporter Scale Factor is set to 1, otherwise the model will import at
a very small scale.
在项目窗口中单击Objects/weapons/rocketLauncher并且确保使FBXImporter Scale Factor(比例因子)被设置为1,否则模型将以一个非常小的比例被导入
Drag Objects/weapson/rocketLauncher model so that it is a child of the RocketLauncher game object.
拖拽Objects/weapson/rocketLauncher模型以便它作为RocketLauncher游戏对象的子对象。