UNITY 3D –游戏编程–第8部分(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/unity-d-game-programming-part-7-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 21 分钟阅读 - 10153 个词 阅读量 0UNITY 3D –游戏编程–第8部分(译文)
原文地址:https://www.codeproject.com/Articles/888403/UNITY-D-Game-Programming-Part-7
原文作者:Vahe Karamian
译文由本站 robot-v1.0 翻译
前言
The eighth article in a series to discuss Unity 3D and how to get started with your own 3D projects.
该系列的第八篇文章讨论Unity 3D以及如何开始自己的3D项目.
介绍(Introduction)
在本文的第八部分中,我们将研究如何改善体验并使我们的游戏创意更具吸引力.我们会在游戏中添加障碍.此外,我们还将介绍声音和灯光效果,这些效果将大大改善环境并使之栩栩如生.(In part eight of the article we will look at how to improve the experience and make our game idea to be more attractive. We will add obstacles in our game play. In addition we will introduce sound and light effects that will enhance the environment considerable and bring it to life.) 如果您尚未这样做,请花一点时间阅读:(If you have not already done so, please take a moment and read:)
-
Unity 3D –游戏编程–第8部分(Unity 3D – Game Programming – Part 8)
Unity 3D网络文章:(Unity 3D Networking Article(s):)
-
Unity 3D-网络游戏编程(Unity 3D - Network Game Programming) Unity 3D Leap Motion和Oculus Rift文章:(Unity 3D Leap Motion and Oculus Rift Article(s):)
-
Unity 3D-Leap Motion集成(Unity 3D - Leap Motion Integration) 在本系列的第一部分中,我们从Unity 3D环境的基础知识入手.感受一下IDE以及将在整个项目中使用的不同部分.我们还介绍了如何使用设计器中的工具将不同的变换应用于选定的游戏对象:定位,旋转和缩放.最后,我们研究了如何创建第一个脚本,并使用该脚本在多维数据集的Y轴上应用了旋转变换.(In the first part of the series we started by the very basics of the Unity 3D environment. Getting a feel of the IDE and the different sections which you will be working with throughout your project. We also covered how to use the tools in the designer to apply different transformation to a selected Game Object: positioning, rotation and scaling. We finally looked at how to create our first script and using the script apply a rotation transform on the Y-Axis of our cube.) 在本系列的第二部分中,我们研究了通过编码对给定对象进行的更多转换.我们还研究了如何创建对于场景中的对象渲染至关重要的光源.(In the second part of the series, we looked at more of the transformation of a given object through coding. We also looked at how to create light sources that are crucial for the rendering of your objects in the scene.) 在本系列的第三部分中,我们研究了如何通过键盘处理用户输入,并根据键码采取了特定的操作.(In the third part of the series we looked at how to process user input through the keyboard and based on the key code take particular actions.) 在本系列的第四部分中,我们着眼于创建一个简单的用户界面.我们开发的用户界面为我们提供了一种向用户反馈的方式,并且为用户提供了输入我们的游戏或模拟游戏的另一种方法.(In the fourth part of the series, we looked at creating a simple user interface. The user interface that we developed provided us a means to feedback to the user, and also another method for the user to input to our game or simulation.) 在第五部分,我们开始了一个简单游戏的想法.我们还研究了如何将3D模型导入游戏引擎.(In the fifth part, we started the idea of a simple game. We also looked at how to import 3D models into the game engine.) 在第六部分中,我们扩展了我们的游戏构想,使其更加有趣和完整.我们介绍了有关游戏和游戏设计的几个概念,并为我们的游戏创建了一个简单的用户界面.(In the sixth part, we expanded on our game idea and making it more interesting and more complete. We introduced several concepts regarding game play and game design as well as created a simple user interface for our game.) 在第七部分中,我们回答了第6部分中提出的一些问题.(In the seventh part, we answered some of the questions that got raised in Part 6.) 在第八部分中,我们将为玩家创造更好的用户体验以及视觉效果.(In part eight, we will be creating a better user experience and also visuals for our players.)
游戏编程简介:使用C#和Unity 3D(Introduction to Game Programing: Using C# and Unity 3D*) (Paperback)*)(平装)( 要么(or*) (eBook)*)(电子书)( 是为帮助对计算机科学和游戏编程领域感兴趣的个人而设计和开发的.它旨在说明计算机编程的概念和基础.它使用简单游戏的设计和开发来说明和应用这些概念.(is designed and developed to help individuals that are interested in the field of computer science and game programming. It is intended to illustrate the concepts and fundamentals of computer programming. It uses the design and development of simple games to illustrate and apply the concepts.) [ ](http://www.lulu.com/commerce/index.php?fBuyContent=18159441) 书号:9780997148404(ISBN: 9780997148404) 版本:第一版(Edition: First Edition) 发行人:Noorcon Inc.(Publisher: Noorcon Inc.) 英语语言(Language: English) 274页(Pages: 274) 装订:装订完美的平装本(彩色)(Binding: Perfect-bound Paperback (Full Color)) 尺寸(英寸):6宽x 9高(Dimensions (inches): 6 wide x 9 tall) -
|
||电子书(ePUB)(eBook (ePUB))
书号(ISBN):9780997148428(ISBN: 9780997148428)
版本:第一版(Edition: First Edition)
发行人:Noorcon Inc.(Publisher: Noorcon Inc.)
英语语言(Language: English)
大小:9.98 MB(Size: 9.98 MB) |
---|
Windows Phone 8.x演示:(Windows Phone 8.x Demo:)
我提供了一个免费的电话应用程序,您可以在Windows Phone上下载和预览演示.要下载移动应用程序,请点击以下链接:(I have provided a free phone application that you can download and preview the demos on your Windows Phone. To download the mobile application, follow the link:) CodeProjectArticleSample(CodeProjectArticleSample)
代码项目文章示例移动应用(Code Project Articles Sample Mobile App)
文章代码和视觉效果的实时预览:(Live Preview of Article Code and Visuals:)
链接到实时预览:(Link to live preview:) http://www.noorcon.com/CodeProject/CodeProjectArticlePreview.html(http://www.noorcon.com/CodeProject/CodeProjectArticlePreview.html)
背景(Background)
**注意:(NOTE:)**对于这个粒子,我将使用SketchUp创建一些简单的构建基块,并将其导入到Unity中!我不是3D建模者或设计师,因此请耐心等待,请原谅!(For this particle, I will be using SketchUp to create some simple building blocks, which I will use to import into Unity! I am not a 3D Modeler or Designer, so please be patient and excuse the mess!) 假定本文的读者通常熟悉编程概念.还假定读者具有C#语言的理解和经验.还建议本文的读者也熟悉面向对象的编程和设计概念.我们将根据需要在本文中简要介绍它们,但是由于它们完全是单独的主题,因此我们将不对其进行详细介绍.我们还假设您有学习3D编程的热情,并且具有3D图形和矢量数学的基本理论概念.(It is assumed that the reader of this article is familiar with programming concepts in general. It is also assumed that the reader has an understanding and experience of the C# language. It is also recommended that the reader of the article is familiar with Object-Oriented Programming and Design Concepts as well. We will be covering them briefly throughout the article as needed, but we will not get into the details as they are separate topics altogether. We also assume that you have a passion to learn 3D programming and have the basic theoretical concepts for 3D Graphics and Vector Math.) 最后,本文使用Unity 3D版本4.6.1,这是截至初始发布日期的最新公共发行版.该系列中讨论的大多数主题都将与旧版本的游戏引擎兼容,也许还与应该在今年某个时候发布的新版本兼容.但是,有一个主题与当前版本的游戏引擎相比在当前4.6.1版本中有显着差异,那就是UI(用户界面)管道.这是由于引擎中的新UI架构远远优于我们在此版本之前所拥有的.首先,我对新的UI架构感到非常满意.(Lastly, the article uses Unity 3D version 4.6.1 which is the latest public release as of the initial publication date. Most of the topics discussed in the series will be compatible with older versions of the game engine, and perhaps also the new version which is supposed to be release sometime this year. There is however, one topics which is significantly different in the current 4.6.1 version compared to the older version of the game engine, and that is the UI (User Interface) pipeline. This is due to the new UI architecture in the engine which is far superior to what we had prior to this release. I for one, am very happy with the new UI architecture.)
使用代码(Using the code)
下载文章系列的项目/源代码:(Downloading the project/source code for article series:) 下载源(Download source) .(.) 随着每篇连续的文章的提交,项目/源代码也将不断扩展.新的项目文件和源文件将包括该系列中的较早部分.(With each consecutive article that is submitted, the project/source code will be also expanding. The new project files and source files will be inclusive of older parts in the series.) **注意:(NOTE:)**要获取最新代码,请转至该系列中最新发布的部分并下载代码.(To get the latest code, go to the most recent published part in the series and download the code.)
改善游戏玩法(Improving the Gameplay)
我们的游戏缺少的一件主要事情是某种障碍.确实,我们的游戏中实际上已经定义了一个障碍,那就是时间.玩家必须遵守游戏开始时给出的奔跑并收集他或她的硬币的时间.这具有挑战性,但是一段时间后它会变老.(One major thing that is missing from our game is some sort of an obstacle. Indeed, we actually have one obstacle already defined in our game, and that is, time. The player is bound to the amount of time given at the beginning of the game to run around and collect his or her coins. This is challenging, but it is going to get old after a while.) 玩家通常想面对一些挑战,而克服挑战就是他们在游戏中实现目标时所获得的满足感.因此,作为游戏设计师,您必须能够创造出具有挑战性的障碍,以保持玩家的机敏和参与度,但又不会过于复杂以阻止玩家的注意力.(Players usually want to face some challenge and overcoming the challenge is the satisfaction one gets when they achieve their objective in a game. Therefore, as a game designer you have to be able to create obstacles that are challenging enough to keep the player alert and engaged, but not overly complicated to discourage the player.) 对于我们的小示例游戏,我想介绍一个这样的挑战.我的想法很简单,但是会进一步增强游戏性.我想介绍一个GameObject,它将用作射击炮弹的基础.如果这些炮弹击中了玩家,则玩家将失败.(For our little example game, I want to introduce one such challenge. The idea I have is rather simple but it will enhance the gameplay a little bit more. I want to introduce a GameObject that will be used as a base to shoot cannon balls. If these cannon balls hit the player, then the player will fail.) 与往常一样,由于我不是图形设计师,所以我将坚持使用简单的模型来说明和实施我们的加农炮!(As always, since I am not a graphics designer, I will be sticking to simple models to illustrate and implement our cannon!)
对加农炮和加农炮球建模(Modeling the Cannon and the Cannon Ball)
对于加农炮,我将使用(For the cannon, I will be using a)多维数据集基本体(Cube Primitive).底座的尺寸为(. The dimensions of the base will be)10厘米x 10厘米x 10厘米(10 cm x 10 cm x 10 cm).换一种说法,(. In other words,)(0.1,0.1,0.1)((0.1,0.1,0.1))(x,y,z)轴上的比例.到我们加农炮的3D视觉方面为止.(for the scale on the (x,y,z) axis. That’s as far as the 3D visual aspect of our cannon goes.)
图1-加农炮基地(Figure 1-Cannon Base) 我还为(I also created a material for the)**大炮游戏对象(Cannon GameObject)**材质可以附加到新创建的(The material can be attached to the newly created)**多维数据集基本体(Cube Primitive)**给它一些颜色(to give it some color.)
模型层次结构简介(Introduction to Model Hierarchy)
我们的(Our)**大炮游戏对象(Cannon GameObject)**外观看起来很简单,但是从层次结构的角度来看,我将更接近其设计方式.为了使我的加农炮能够按照我预期的方式工作,我需要再介绍两个(visually looks simple, but the way I am approaching its design, it will be a little more involved from a hierarchical perspective. In order to make my cannon work the way I have envisioned, I would need to introduce two more)**游戏对象(GameObjects)**加农炮的结构.我叫一个(to the structure of the cannon. I call one)**加农炮基地(CannonBase)**和另一个(and the other)CannonStart(CannonStart).(.)
这两个GameObject是所谓的(These two GameObjects are what’s called)空的(Empty)游戏对象(GameObject).也就是说,它们没有任何视觉上的组成部分.默认情况下,它们只有一个(. That is, they do not have any visual component to them. By default they only have a)转变(Transform)附加到它们的组件.(component attached to them.)
我们将使用(We will be using)**空的游戏对象(Empty GameObjects)**在我们加农炮的几个重要部分第一,(for a few important parts of our cannon. First,)**加农炮基地(CannonBase)**将用于旋转我们的大炮的喷嘴.第二,(will be used to rotate the nozzle of our cannon. Second,)**CannonStart(CannonStart)**将用于指示炮弹的起始位置.(will be used to indicate the starting position of our cannon ball.)
图2:Cannon GameObject的结构(Figure 2-Structure of Cannon GameObject)
看一下图2.注意,(Take a look at Figure 2. Notice, that the)大炮游戏对象(Cannon GameObject)有两个孩子(has two child)游戏对象(GameObjects).这些子对象是我们的(. These child objects are our)**空的游戏对象(Empty GameObjects)**代表(representing the)加农炮基地(CannonBase)和(and)CannonStart(CannonStart).(.)
**注意:(NOTE:)**子GameObjects的位置很重要.当您使一个GameObject成为另一个GameObject的子代时,子GameObject的变换是相对于父GameObject的.这非常重要,您必须了解这种关系和相对位置.(Positioning of the child GameObjects are important. When you make a GameObject a child of another GameObject, the transform of the child GameObject is relative to the parent GameObject. This is very important and you must be aware of the relation and relative position.)
下图将说明代表加农炮的每个GameObject的"检查器"窗口:(The following figures will illustrate the Inspector Window for each GameObject representing the Cannon:)
|
|
|
![]() |
|
|
|
我已将这些数字作为视觉线索提供.请注意(I have made the figures available as a visual clue. Notice that the)大炮游戏对象(Cannon GameObject)作为(as the)父母(parent)游戏对象(GameObject)坐在一个位置(is sitting at a position of)(-3.892,0.05,1.082)((-3.892, 0.05, 1.082)),(,)佳能基础游戏对象(CannonBase GameObject)这是一个(which is a)儿童(child)的(of the)大炮游戏对象(Cannon GameObject)坐在一个位置(is sitting at a position of)(0,0.55,0)((0, 0.55, 0)).这个位置是相对于(. This position is relative to the)父GameObject(parent GameObject).所以这意味着(. So this means, that)加农炮基地(CannonBase)是(is)0.55米(0.55 meters)在父母中心上方(above the center of the parent)游戏对象(GameObject).然后,(. Then,)CannonStart GameObject(CannonStart GameObject)是的孩子(is a child of)佳能基础游戏对象(CannonBase GameObject)坐在一个位置(is sitting at a position of)(0,0.1,0)((0, 0.1, 0)),相对于(, relative to its)父对象(parent object).另请注意,(. Also note, that the)**CannonStart GameObject(CannonStart GameObject)**旋转了(has a rotation of)X轴347度(347 degrees on its X-Axis).您很快就会明白原因.(. You will figure out why shortly.)
**注意:(NOTE:)**通常,做一个(Usually it is a good idea to do a)重启(Reset)当您第一次将子对象附加到其父对象时,将其放在子对象上.这是通过使用(on the child GameObject when you first attach it to its parent. This is done by using the)设定值(Settings)从下拉菜单(drop-down from the)检查器窗口(Inspector Window)为了(for the)变换组件(Transform Component)所选GameObject的.这会将其移动到父GameObject的中心.然后,您可以进行相应的调整.(of the selected GameObject. This will move it to the center of the parent GameObject. Then you can adjust accordingly.)
图9重置子GameObject(Figure 9-Reset Child GameObject) 现在,我们对加农炮模型感到满意,是时候用一些魔法将其变为现实了!是的,您猜对了,编程!(Now that we are happy with our cannon model, it is time to bring it to life with some magic! Yes, you guessed it, programming!)
using UnityEngine;
using System.Collections;
public class LaunchCannon : MonoBehaviour {
public float canonBallSpeed = 0.5f; // spped of our cannon ball
public float rateOfFire = 5.5f; // cannon blast rate in seconds
public float fireDelay;
public GameObject cannonBall; // used to store our cannon ball prefab
public GameObject cannonBase; // used to store our cannon base
public GameObject cannonStart; // used to store our cannon nozle starting point
// Use this for initialization
void Start () {
this.canonBallSpeed = Random.Range (3, 4); // randomize cannon ball speed between 3 and 4
this.rateOfFire = Random.Range (3, 6.5f); // randomize rate of fire between 3 and 6.5 seconds
// set a fire delay time for the cannon
this.fireDelay = Time.time + this.rateOfFire;
}
// Update is called once per frame
void Update () {
// if the current game running time is larger then our fire delay time, then fire!
if (Time.time > this.fireDelay){
// cannon ball speed and rate of fire will be randomly generated based
// on the defined boundaries ...
this.canonBallSpeed = Random.Range (3, 4);
this.rateOfFire = Random.Range (3, 6.5f);
// re-set our fire delay
this.fireDelay = Time.time + this.rateOfFire;
// instantiate our cannon ball at the specified location
GameObject fire = GameObject.Instantiate(this.cannonBall,
this.cannonStart.transform.position,
this.cannonStart.transform.rotation) as GameObject;
// self-destruct our cannon ball after 10 seconds
Destroy(fire, 10.0f);
// use the physics engine to push/give velocity to our cannon ball
fire.rigidbody.velocity = this.cannonStart.transform.TransformDirection(
new Vector3(0,this.canonBallSpeed,0));
}
// rotate the cannon base around its Y-Axis continiously
this.cannonBase.transform.Rotate (Vector3.up, 10 * Time.deltaTime);
}
}
该脚本非常简单.让我们来看看它.我们有以下(The script is pretty simple. Let’s take a look at it. We have the following)**游戏对象(GameObject)**定义的变量:(variables defined:)炮弹(cannonBall),(,)加农炮基地(cannonBase)和(and)cannonStart(cannonStart).变量名称应该是自描述的.变量(. The variable names should be self-descriptive. The variable)**炮弹(cannonBall)**是对表示我们的加农炮球的预制件的参考,变量(is a reference to the prefab that will represent our cannon ball, the variable)**加农炮基地(cannonBase)**是对大炮模型中大炮基础的Empty GameObject的引用,并且(is a reference to the cannon base Empty GameObject in the cannon model, and)**cannonStart(cannonStart)**是对大炮模型中大炮启动Empty GameObject的引用.(is a reference to the cannon start Empty GameObject in the cannon model.) 当大炮准备起飞时,变量炮弹用于引用和实例化我们的炮弹.变量(The variable cannonball is used to reference and instantiate our cannon ball when the cannon is ready to go off. The variable)**cannonStart(cannonStart)**用于指示炮弹实例化时的初始位置.变量(is used to dictate the initial position of the cannon ball when it is instantiated. The variable)**加农炮基地(cannonBase)**用于旋转自身和附加的子对象(is used to rotate itself and the attached child object)**cannonStart(cannonStart)**在Y轴上连续(continuously on the Y-Axis.) 注意在(Notice in the)开始()(Start())和(and the)更新()(Update())我们设置的功能(functions we set the)**炮速(cannonBallSpeed)**和(and the)**射速(rateOfFire)**变量生成随机生成的数字.这样做是为了使大炮的行动和感觉独立而随意,使其更加有趣.(variables to a randomly generated number. This is done to make the cannon’s act and feel independently and at random to make it more interesting.) 另一个需要注意的因素是,我们有一个计时器设置,只有满足计时器条件时,大炮才会发射.的(Another element to notice is that we have a timer set and the cannon only fires if the timer condition is met. The)如果(Time.time> this.fireDelay)(if (Time.time > this.fireDelay))声明为我们解决了这一问题.(statement takes care of that for us.)时间(Time.time)是游戏的实际运行时间!它从0…N开始,N是退出或终止游戏的时间.变量(is the actual running time of the game! It starts from 0 … N, N being when you exit or terminate the game. The variable)fireDelay(fireDelay)设置为(is set to be the addition of the)射速(rateOfFire)和当前(and current)时间(Time.time).这样可以确保大炮不会在大炮弹之后连续发射大炮弹,也不会用大炮弹淹没游戏!要了解差异,只需注释掉(. This will make sure that the cannon does not fire continuously cannon ball after cannon ball and flood the game with cannon balls! To see the difference, just comment out the)如果()(if())声明.(statement.) 需要注意的另一件事是:(One more very important thing to notice:)消灭(火,10.0f);(Destroy(fire, 10.0f);)用于销毁指定的游戏对象.你能猜出为什么这很重要吗?(is used to destroy the specified game object. Can you guess why this is important?) 想象一下,假设您的大炮每2秒炸一次炮弹.这意味着我们每两秒钟创建一个新的炮弹GameObject.因此,在10秒内,场景中将有5个炮弹,在60秒中,我们将有30个炮弹,在5分钟内,场景中每个大炮将产生150个炮弹!(Imagine the following, assume that your cannon is blasting cannon balls every 2 seconds. That means every two seconds we create a new cannon ball GameObject. So in 10 seconds we will have 5 cannon balls in the scene, in 60 seconds we will have 30, and in 5 minutes we will have 150 cannon balls generated per cannon in the scene!) 现在假设我们在关卡中放置了5门大炮,这将在5分钟内生成750枚炮弹!现场有很多炮弹!它不仅不允许玩家从游戏角度出发,而且还会浪费大量内存和CPU能力.请记住,这些对象是(Now assume we place 5 cannons in our level, this would generate 750 cannon balls in 5 minutes! That is a lot of cannon balls in the scene! It will not only not allow the player to get around from a gameplay point of view, but it will also waste a lot of memory and CPU power. Remember, that these object are)刚体(rigidbody)对象,并且每个对象都与物理引擎进行交互,因此,游戏引擎必须不断计算和计算所有这些游戏对象在内存中时的实际交互作用!因此,在开发关卡时需要考虑到这一点.(objects and each one interacts with the physics engine, therefore, the game engine has to calculate and compute the actual interaction of all of these game objects continuously while they are living in the memory! So you will need to take that into consideration when developing your level.) 所以(So the)销毁(GameObject,TimeInSeconds)(Destroy(GameObject,TimeInSeconds))函数将在指定的时间(秒)后从内存中删除引用的GameObject!在我们的案例中,每个炮弹在游戏中的寿命为10秒!(function will remove from memory the referenced GameObject after a specified time in seconds! In our case, each cannon ball has a lifespan of 10 seconds in the game!)
炮弹(The Cannon Ball)
炮弹本身无非是(The cannon ball itself is nothing more than a)球形图元(Sphere Primitive).我已将其在所有三个轴上均缩小为(0.05,0.05,0.05).套用(. I have scaled it down to (0.05, 0.05, 0.05) on all three axis. Apply a)刚体(Rigidbody)并将"插值"选项设置为"插值".我介绍的另一个组件是(to it and set the Interpolate option to Interpolate. One more component I have introduced was the)音源(Audio Source).当您将音频源连接到GameObject时,便能够从GameObject生成声音,在这种情况下,我们希望大炮能够为我们提供射击大炮球的音效,因此,最简单的应用声音的方法是将其应用于炮弹本身.现在,您可以将音频剪辑应用于音频源,并在需要时播放.(. When you attach an Audio Source to a GameObject, you are able to generate sound from the GameObject, in this case, we want our cannon to give us a sound effect of shooting a cannon ball, and therefore, the easiest way to apply sound is to apply it to the cannon ball itself. Now you can apply an audio clip to the audio source and have it play when needed.)
|
图10-CannonBall的刚体(Figure 10-Rigidbody for CannonBall)
|
图11-CannonBall的音频源(Figure 11-Audio Source for CannonBall)
完成并满意您的炮弹后,继续进行(Once you are done and satisfied with your cannon ball, go ahead and make a)预制件(Prefab)它的.您将需要将此Prefab分配给上述脚本.(of it. You will need to assign this Prefab to the script described above.)## 改善场景并将所有内容放在一起(Enhancing the Scene and Putting Everything Together)
我继续进行,还添加了四个其他光源,并将每个光源放置在一个角落.由于我的关卡没有任何纹理,因此我认为为其提供一些灯光效果将在视觉上增强它的效果,并且还可以用作玩家了解其在关卡中位置的视觉线索.(I have gone ahead and also added four additional light sources, and places each one at one of the corners. Since, my level does not have any textures, I thought giving it some light effects will enhance it visually, and also can be used as a visual clue for the player to know where they are in the level.)
图12-添加光源等(Figure 12-Adding Light Sources and Etc…) 当您开始扩展构思并引入新功能时,这可能会派上用场.我添加了四个点光源,它们具有红色,蓝色,橙色和绿色的独特颜色.(This can come handy as you start expanding on the idea and introducing new features. I added four point lights with unique colors of Red, Blue, Orange and Green.) 下一步是将脚本附加到(The next step was to attach the script to the)大炮游戏对象(Cannon GameObject).附加后,我继续将以下变量分配给它们各自的元素:(. Once attached, I went ahead and assigned the following variables to their respective elements:)炮弹(cannonBall),(,)加农炮基地(cannonBase),(,)cannonStart(cannonStart).如果您还记得,炮弹是对(. If you recall, cannonball is a reference to the)炮弹预制(CannonBall Prefab),cannonBase和cannonStart是对Cannon GameObject层次结构上的子对象的引用!完成后,我也将Cannon GameObject制成了Prefab.(, cannonBase and cannonStart are a reference to the child objects on the Cannon GameObject hierarchy! Once done, I made the Cannon GameObject into a Prefab as well.) 现在,您可以根据需要在现场多次拖放Cannon Prefab.我已经放了五门大炮.您可以从图12直观地看到它们的位置,因为它们具有粉红色的色调.(Now, you can drag and drop your Cannon Prefab as many times as you would like on the scene. I have placed 5 Cannons. You can visually see where they are from Figure 12 as they have pinkish tone to them.) 我要做的最后一件事是将另一个音频源添加到CP(角色播放器),并将背景级音乐附加到此音频源.因此,现在开始游戏时,您会听到背景水平的音乐以及发射时炮弹的声音效果.(One last thing I have done is add another Audio Source to the CP (Character Player) and I have attached the background level music to this Audio Source. So now when you start the game, you hear the background level music and also the sound effects of the cannon ball when they are fired.)
兴趣点(Points of Interest)
到目前为止,我们还没有为炮弹创造一种能够摧毁玩家或对玩家造成一定伤害的手段.下一步将是提出一种机制,如果他们被炮弹击中,则会损坏我们的玩家.(We did not create a means for the cannon balls to destroy the player or cause some damage to the player as of yet. The next step would be to come up with a mechanism to damage our player if they are hit by the cannon balls.)
历史(History)
这是系列文章的第八篇,我将慢慢为Code Project社区做出贡献.(This is the eighth article of a series which I would slowly contribute to the Code Project community.)
-
Unity 3D –游戏编程–第8部分(Unity 3D – Game Programming – Part 8)
Unity 3D网络文章:(Unity 3D Networking Article(s):)
-
Unity 3D-网络游戏编程(Unity 3D - Network Game Programming) Unity 3D Leap Motion和Oculus Rift文章:(Unity 3D Leap Motion and Oculus Rift Article(s):)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C#5 C# .NET Mobile iPhone Android Windows Design Dev 新闻 翻译