UNITY 3d FPS游戏教程中文版2
时间:2025-04-20
时间:2025-04-20
2
02
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.
增加mainLevelMesh和FPS控制预制物体到场景中。
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:这个脚本代码:
This function initializes weapon 0 as the default weapon.
这个函数初始化武器0 为缺省的武器。
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. 这个函数检测键盘输入;开火按钮, 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游戏对象的子对象。 The code for the RocketLauncher.js script is as follows:
随后是RocketLauncher.js的脚本代码:
This code ensures that the weapon can’t fire faster than reloadTime. It also checks that the user can fire only when they have sufficient ammo.
这段代码确保了武器开火速度不能比重装(弹药)的时间还快,它还可以检查用户是否有足够的弹药开火
The behavior for the RocketLauncher is similar to that in the previous FPS tutorial with the exception of the reload time and ammo count described above.
这个RocketLauncher的行为与以前的FPS教程中的相似,除 …… 此处隐藏:16415字,全部文档内容请下载后查看。喜欢就下载吧 ……
上一篇:工作台振动测量仪设计