Unity3d_FPS游戏教程2
- 格式:doc
- 大小:2.18 MB
- 文档页数:25
FPS游戏教程2Part 2: Enhancements第二部分增强This intermediate-level tutorialextends upon the Basic FPS tutorialby introducing game elements suchas multiple weapons, damage andenemies.这个中级教程是FPS基本教程的扩展,介绍游戏元素例如多种武器、毁伤和敌人。
PrerequisitesThis tutorial assumes that you are familiar with the Unity interface and basic scripting concepts. Additionally, you should already be familiar with the concepts discussed inPart 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 youhave 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 thatwere 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 objectto Main Camera (inside FPS controller). Our weapons will be added as childrenof 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 destroyedwhenever 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 surethat the FBXImporter Scale Factor is set to 1, otherwise the model will import ata 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的脚本代码:var projectile : Rigidbody;var initialSpeed = 20.0;var reloadTime = 0.5;var ammoCount = 20;private var lastShot = -10.0;function Fire (){// Did the time exceed the reload time?难道时间超过重载时间了?if (Time.time > reloadTime + lastShot && ammoCount > 0){// create a new projectile, use the same position and rotation as the Launcher.//就像发射器一样,在同样的位置建一个新的发射体var instantiatedProjectile : Rigidbody = Instantiate (projectile,transform.position, transform.rotation);// Give it an initial forward velocity. The direction is along the z-axis of// the missile launcher's transform.//给它一个初始方向速度,沿着导弹发射器的Z轴instantiatedProjectile.velocity = transform.TransformDirection(This code ensures that the weapon can’t fire faster than reloadTime. It also checks thatthe 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教程中的相似,除了上面所说的重载时间和弹药计数以外。