UNITY 3D –游戏编程–第5部分(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/unity-d-game-programming-part-4-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 24 分钟阅读 - 11974 个词 阅读量 0UNITY 3D –游戏编程–第5部分(译文)
原文地址:https://www.codeproject.com/Articles/882200/UNITY-D-Game-Programming-Part-4
原文作者:Vahe Karamian
译文由本站 robot-v1.0 翻译
前言
The fifth article in a series to discuss Unity 3D and how to get started with your own 3D projects.
该系列的第五篇文章讨论Unity 3D以及如何开始自己的3D项目.
介绍(Introduction)
在本系列的第五部分中,我们将汇集到目前为止所学的一切,并将其提升一个档次!我们将研究如何将3D模型导入到Game Engine中,以及如何使用到目前为止所学到的方法将其应用于世界范围内的运动.(In part five of the series, we will bring together everything we have learned thus far, and move it up by a notch! We will look at how to import a 3D Model into the Game Engine and also using the methods we have learned so far, apply them to the movement in and around the world.) 如果您尚未这样做,请花一点时间阅读:(If you have not already done so, please take a moment and read:)
-
Unity 3D –游戏编程–第5部分(Unity 3D – Game Programming – Part 5)
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.) 在第5部分中,我们将利用到目前为止所学到的一切来创建一个有趣的环境,使玩家角色可以四处移动并发现对象.同样,本文的重点将是将3D模型导入游戏引擎的方法.(In Part 5, we are going to use everything we have learned so far to make an interesting environment, where a player character can move around and discover object. Also, the key point in this article will be the method of importing a 3D model into the Game Engine.)
游戏编程简介:使用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.)
想法(The Idea)
因此,与在宇宙中进行的任何其他重大成就和计划一样,第一步将是(So as any other great achievement and project that is going on in the universe, the first step would be to come up with an)理念(Idea), 一种(, a)视力(Vision)之类的.因此,出于本文的目的以及到目前为止我们在系列中所做的一切,我们谨提出以下建议!(of sort. So, for the purposes of this article and everything we have been doing in the series up-to this point, we would like to propose the following!)
简要:(Brief:)
创建一个类似于迷宫的关卡.该级别应足够大,以使玩家能够自由移动并发现该级别以及该级别的所有相关元素.该级别将是有限的,并且将具有由墙定义的边界.假设我们希望外面的楼层由总共100平方米组成,因此我们的主要楼层将为10米x 10米见方的面积.玩家的目标是在给定的时间内拾取尽可能多的硬币.(Create a level that will resemble a maze. The level should be big enough to enable the player to freely move around and discover the level and all associated elements of that level. The level will be finite, and will have boundaries defined by walls. Let’s assume we want out level to be composed of a total of 100 square meters, therefore our main level will be a 10 meter x 10 meter square area. The objective of the player will be to pick up as many coins as possible in a given time.)
**注意:(NOTE:)**无论如何,如果您随时想为该系列的任何部分做出贡献,请这样做,并与我联系,以便我们也可以将您的想法和内容纳入该系列!(By all means, if you, at any time, want to contribute to any part of the series, please do so, and contact me so that we can incorporate your ideas and content into the series as well!)
让我们首先勾勒出我们的环境,再说一次,我没有像您大多数人那样富有创造力,我想到了以下几点:(Let’s start by sketching out our environment, again me not being as creative as most of your out there, I came up with the following:)
**注意:(NOTE:)**这不是SketchUp或3D建模文章,因此我将不介绍该应用程序的使用.(This is not a SketchUp or a 3D Modeling article, so I will not be covering the use of the application.)
图1-我的关卡的SketchUp模型(Figure 1-SketchUp Model for My Level) 我使用SketchUp为本演示创建了所需的迷宫.由于我不是3D设计人员/建模人员,因此使用起来很容易.您可以使用自己喜欢的任何其他工具.所以这是步骤:(I used SketchUp to create the desired maze I want for this demo. It is easy for me to use and since I am not a 3D Designer / Modeler. You can use any other tool that you are comfortable with. So here are the steps:)
-
使用3D建模工具设计关卡,以使您满意.(Design the level in a 3D Modeling tool to your satisfaction.)
-
将模型导出为.OBJ文件.(Export your model as an .OBJ file.)
**注意:(NOTE:)**在3D建模软件中,您可以将纹理附加到模型上.我尚未在模型上附加任何纹理.在这一点上,我只对模型的结构感兴趣.(In your 3D modeling software, you are capable of attaching textures to your model. I have not attached any textures to my model. At this point I am only interested with the structure of the model.) **注意:(NOTE:)**导出模型时,请确保选择以下选项:选中"对所有面进行三角测量",选中"导出两面",并同时选中"导出边缘".如果您在模型上附加了任何纹理,则还要选中"导出纹理贴图"选项.(When you are exporting your model make sure that you select the following options: Triangulate all faces is checked, Export two-sided faces is checked, and export the edges is checked as well. If you have attached any textures to your model, also check-mark the Export texture maps option.) 您需要了解的另一个重要点是为建模软件配置的实际单元.由于Unity 3D以公制单位表示,因此最好将3D建模软件配置为使用公制单位进行建模.(Another important point you need to be aware of is the actual unit your modeling software is configured for. Since Unity 3D is in metric units, it is best to configure your 3D modeling software to use metric units for modeling.) **注意:(NOTE:)**导出模型时,还要确保以公制单位导出.如果您的模型已经是公制单位,请选择"模型单位".(When exporting your model, also make sure that you are exporting in metric units. If your model is already in metric units, select Model Units.) 现在,让我们看看将模型导入Unity 3D有多么容易.(So now let’s see how easy it is to import our model into Unity 3D.)
导入模型:(Importing the Model:)
我想做的第一件事是创建一个目录结构,该目录结构在项目中易于导航.我保持它很简单.对于项目中将要使用的每种不同类型的资产,我都有一个文件夹.因此,一个给定的项目至少在以下文件夹下(The first thing I would like to do is create a directory structure that would be easy for navigation within the project. I keep it pretty simple. I have a folder for each different type of asset I will be using in my project. So a given project has at least the following folder under the)资产(Assets)夹:(folder:)
-
楷模(Models)–我所有的模型都保存在此文件夹中.(– all my models are kept in this folder.)
-
预制件(Prefabs)–我所有的预制件都保存在此文件夹中.(– all my prefabs are kept in this folder.)
-
场景(Scenes)–我所有的场景都保存在此文件夹中.(– all my scenes are saved in this folder.)
-
剧本(Scripts)–我所有的脚本都保存在此文件夹中.(– all my scripts are saved in this folder.)
-
精灵(Sprites)–我所有的精灵(2D纹理)都保存在此文件夹中.(– all my sprites (2D textures) are saved in this folder.)
您可以直接在Unity 3D内部或在存储项目的文件系统级别上创建此目录结构.要在IDE中创建它,您需要右键单击Project窗口,然后从上下文菜单中选择Create-> Folder.(You can create this directory structure directly from within Unity 3D, or at the file system level, where your project is stored. To create it within the IDE, you will need to Right-Click in the Project window and select Create->Folder from the Context Menu.)
图2-演示的项目结构(Figure 2-Project Structure for Demo)
要导入模型,只需将.OBJ文件复制并粘贴到models文件夹中即可.可以通过系统的文件浏览器复制和粘贴任何其他文件来完成.将.OBJ文件复制到所需的文件夹后,请返回Unity 3D,引擎将自动将适当的内部标记应用于您的一个或多个.OBJ文件.(To import your model, you can simply copy and paste your .OBJ file in the models folder. This can be done through your system’s file explorer to copy and paste any other file. Once you copy the .OBJ file in the desired folder, come back to Unity 3D, and the engine will automatically apply the proper internal markings to your .OBJ file or files.)
图3-在模型文件夹中放置.OBJ文件后的项目视图(Figure 3-Project View after Placing .OBJ File in Model Folder)
|
图4-模型检查器窗口(Figure 4-Inspector Window for Model) |导入模型后,从(After you import your model, from the)项目窗口(Project Window),导航到您的模型并选择它.(, navigate to your model and select it.) 你的(Your)检查器窗口(Inspector Window)现在应该显示模型的所有属性.(should now display all of the properties for your model.) 如图4所示,有很多属性,每个属性都很重要.默认情况下选中了一些.根据您的模型和模型的用途以及目的,您将根据需要打开或关闭这些属性.您将需要自己做更多的阅读.(There is a lot of attributes as shown in Figure 4, and each one is important. Some are checked by default. And depending on your model and the use of your model, the purpose, you will turn on or off these attributes as you see fit. You will need to do more reading on this on your own.) 但是出于我们的目的,并且在大多数情况下,默认设置已足够.我们需要做的唯一更改是启用(But for our purposes, and in most cases, the default settings are sufficient. The only change we need to do is enable the)产生碰撞(Generate Colliders)功能,如图4所示,项目编号为2.(feature, as shown in in Figure 4, item number 2.) 对撞机是模型的重要组成部分,稍后我们将详细介绍.对撞机用于碰撞检测.(The colliders are an important part of the model, and we will cover this in more details later. The colliders are used for collision detection.) 接下来的两个重要属性是(The next two important attributes are the)法线(Normals)和(and)切线(Tangents)自动导入并由引擎自动计算,图4中的数字3.(which are imported automatically and calculated automatically by the engine, number 3, in Figure 4.) 在里面(In the)检查器窗口(Inspector Window),一直向下滚动,然后单击(, scroll all the way down and click on the)应用(Apply)按钮.现在,Unity将所有必要的更改应用于模型.(button. Now Unity will apply all of the necessary changes to your model.)
创建关卡:(Creating the Level:)
让我们通过创建新场景来开始新的水平.您可以通过选择(Let’s begin our new level by creating a new scene. You can do this by selecting)**文件->新场景(File->New Scene)**从主菜单.(from the main menu.)
**注意:(NOTE:)**如果当前场景没有保存,将提示您保存它.(If your current scene is not saved, you will be prompted to save it.)
现在将模型从"项目"窗口拖放到Scene Designer视图中.(Now drag and drop your model from the Project Window into the Scene Designer View.)
图5-在设计视图中导入的模型(Figure 5-Imported Model in Design View) 我已经调整了视图和摄影机,以更好地了解关卡在3D空间中的外观.另外,我所做的是将级别模型定位到以下坐标:<5,0,5>;因为我的模型是10 x 10,所以从技术上讲,该模型将模型定为原点.(I have adjusted my view and the camera to get a better idea of how the level look in the 3D space. Also, what I did is position the level model to the following coordinates: <5,0,5>; this technically centers the model to the origin, since my model is 10 x 10. I also placed a light source at the origin to give some light to the scene for better viewing.) 就将模型放置在场景中而言,这就是您要做的所有事情.至此,我们完成了这个阶段.现在,让我们看看游戏的下一步.(That’s all you need to do as far as placing the model in the scene is concerned. So we are done with this stage. Now let’s look at the next step in our game.)
创建并放置玩家角色:(Creating and Placing the Player Character:)
因为我们的游戏是由迷宫组成的,并且游戏中的玩家应该在给定的时间内收集尽可能多的硬币,所以我决定将玩家建模为球体.因此,我们将使用球形图元作为主要角色的占位符.为此,在(Since our game is composed of a maze, and the player in the game is supposed to gather as many coins as possible in a given amount of time, I have decided to model my player as a sphere. So we will use a sphere primitive as a place holder for our main character. To do so, in the)层次窗口(Hierarchy Window)右键单击并选择(right-click and select)3D对象->球形(3D Object->Sphere).(.)
图6-创建球体作为玩家角色的占位符(Figure 6-Create Sphere as Placeholder for Player Character)
**注意:(NOTE:)**创建Sphere对象时,请使用设计定位工具将其正确放置在关卡模型上方和内部.(When you create your Sphere object, use the design positioning tools to place it properly above and within the level model.)
|
图7-播放器占位符的检查器窗口(Figure 7-Inspector Window for Player Placeholder) |你的(Your)检查器窗口(Inspector Window)现在应该显示模型的所有属性.(should now display all of the properties for your model.) 我想做的第一件事是将GameObject的名称从(First thing I would like to do is change the name of the GameObject from)领域(Sphere)至(to)CharacterPlaceHolder_Part_5(CharacterPlaceHolder_Part_5).(.) 我们需要对此GameObject做的第二个也是更重要的更改是添加一个(The second and more important change that we need to do to this GameObject is to add a)刚体(Rigidbody)组件.(component to it.) 为此,您需要选择(In order to do so, you will need to select)添加组件(Add Component)中的按钮(button in the)检查器窗口(Inspector Window),然后选择(, and select)物理(Physics)接着(and then)刚体(Rigidbody).(.) 添加一个(Adding a)刚体(Rigidbody)组件的运动会将其运动置于Unity物理引擎的控制之下.即使不添加任何代码,(component to an object will put its motion under the control of Unity’s physics engine. Even without adding any code, a)刚体(Rigidbody)如果还存在正确的Collider组件,则重力将向下拉动对象,并对与传入对象的碰撞产生反应.(object will be pulled downward by gravity and will react to collisions with incoming objects if the right Collider component is also present.) 这足以满足我们正在寻找的模拟运动以及与场景中其他对象碰撞的需求.(This will be enough for what we are looking for simulating the motion and also collision with other objects in the scene.) **注意:(NOTE:)**我在X和Z轴上设置了"冻结旋转".(I have set the Freeze Rotation on the X and Z Axis.) 这是基于在环境中产生的力来阻止对象沿那些轴旋转.例如,我们不希望对象与另一个对象碰撞时在X轴和Z轴上旋转.(This is to stop the object rotate in those axis based on the forces developed within the environment. For example we don’t want our object to do rotation on the X and Z Axis if it collides with another object.)
我想做的下一件事是能够移动(The next thing I want to do is be able to move the)角色播放器(CP)(Character Player (CP))在现场.为此,我们需要创建一个脚本并将其附加到代表我们的对象(in the scene. In order to do this, we need to create a script and attach it to the object representing our)CP(CP).我叫那个剧本(. I call that script)**playerInput.cs(playerInput.cs)**这是它的清单:(and here is the listing for it:)
using UnityEngine;
using System.Collections;
public class playerInput : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// code for the movement of player (CP) forward
if(Input.GetKey(KeyCode.UpArrow)){
this.transform.Translate(Vector3.forward * Time.deltaTime);
}
// code for the movement of player (CP) backward
if(Input.GetKey(KeyCode.DownArrow)){
this.transform.Translate(Vector3.back * Time.deltaTime);
}
// code for the movement of player (CP) left
if(Input.GetKey(KeyCode.LeftArrow)){
this.transform.Rotate(Vector3.up, -5);
}
// code for the movement of player (CP) right
if(Input.GetKey(KeyCode.RightArrow)){
this.transform.Rotate(Vector3.up, 5);
}
}
}
我们的CP运动确实非常简单.我们将使用键盘箭头来定义CP的运动.向上和向下键盘箭头将分别使CP向前和向后移动,而向左和向右键盘箭头将分别向左或向右旋转CP.(Our CP movement is very simple really. We will be using our keyboard arrows to define the movement of the CP. The Up and Down keyboard arrows will move the CP forward and backward respectively, and the left and right keyboard arrows will rotate the CP left or right respectively.)
我想对场景(级别)做的最后一件事是使摄像机跟随CP.这也很容易实现!(One more last thing I would like to do to our scene (level) is to make the Camera follow the CP. This is also very easy to achieve!)
在场景设计器中,使用鼠标将视图放置在CP(球体)后面的场景设计器中.(In the scene designer, use your mouse to position your view in the scene designer behind the CP, the Sphere.)
图8:游戏视图显示了游戏过程中的实际摄像机视图(Figure 8-Game View Showing Actual Camera View during Game Play)
当您对所看到的内容感到满意时(When you are satisfied with what you see in the)场景窗口(Scene Window),选择(, select the)主相机(Main Camera)目的.从主菜单中选择(object. From the main menu, select)GameObject->与视图对齐(GameObject->Alighn With View).这将移动(. This will move the)主相机(Main Camera)GameObject或当前选择的GameObject要与场景中的视图对齐!(GameObject, or the current selected GameObject, to be aligned with the view in the scene!)
您现在可以运行该程序,并能够按设计的级别移动:(You can now run the program and be able to move in the designed level:)
创建硬币:(Creating the Coins:)
好了,现在我们已经有了我们的主要关卡设计和(Ok so now that we have our main level design in place and our)CP(CP)也就位并且可以移动,我们就可以开始制作硬币了!为了设计硬币,我使用了(also in place and movable, we can start working on the coins! To design the coin, I have used the)胶囊(Capsule)原始.因此,要在您的(primitive. So to create the coin, in your)层次窗口(Hierarchy Window),右键单击以获取(, right-click to get the)上下文菜单(Context Menu),然后选择(, and select)3D对象->胶囊(3D Object->Capsule).(.)
图9-修饰后的胶囊原语(Figure 9-Capsule Primitive after Modifications)
这将在场景中放置一个胶囊原语.我们需要修改胶囊游戏对象的属性,使其看起来像硬币,为此,我们需要使用(This will place a capsule primitive on the scene. We would need to modify the properties of our capsule game object to make it look like a coin, to do so, we will need to work with the)检查器窗口(Inspector Window).(.)
|
图10-Coin GameObject Inspector窗口(Figure 10-Coin GameObject Inspector Window) |你的(Your)检查器窗口(Inspector Window)现在应该显示模型的所有属性.(should now display all of the properties for your model.) 我想做的第一件事是将GameObject的名称从(First thing I would like to do is change the name of the GameObject from)胶囊(Capsule)至(to)硬币(coin).(.) 我要进行的第二个更改是更改(The second change I would like to make is to change the)规模(Scale)GameObject的属性.我想使它看起来更像一枚硬币,所以要这样做,我必须将比例更改为以下值:(attributes of the GameObject. I want to make it look more like a coin, so in order to do so, I have to change the scale to the following:)<0.25,0.05,0.25>(<0.25, 0.05, 0.25>)每个轴分别.同时将GameObject沿其Z轴旋转90度.轮换应为:(respective to each axis. Also rotate the GameObject 90 degrees on its Z-Axis. Rotation should be:)<0,0,90>(<0,0,90>).(.) 第三个重要的属性更改是设置(The third important property change is to set the)是触发(Is Trigger)胶囊对撞机上的属性(property on the Capsule Collider to)真正(True).这对于发动机中的碰撞检测很重要.当此GameObject的碰撞器撞击/进入另一个GameObject的碰撞器时,它允许引发事件.(. This is important for collision detection in the engine. It allows to raise an event when the collider of this GameObject hits/enters the collider of another GameObject.) 最后,我想使硬币在场景中旋转,以达到某种动态效果并在关卡中产生额外的效果.因此,我们将旋转脚本附加到(Lastly, I would like to make the coin rotate in the scene for some dynamism and extra effect in the level. Therefore, we will attach a rotation script to the)硬币游戏物体(coin GameObject)叫(called)rotationCoin.cs(rotateCoin.cs).(.) **注意:(NOTE:)**由于我必须将原始的Capsule图元沿Z轴旋转90度,因此硬币对象的旋转轴不再位于Y轴上.根据我的设计,新的旋转轴将为X轴.您的可能会有所不同!(Since I had to rotate my original Capsule primitive on it’s Z-Axis 90 degrees, the rotation axis of the coin object is not going to be on the Y-Axis anymore. The new axis of rotation will be the X-Axis based on my design. Yours might be different!)
**注意:(NOTE:)我还创建了一个名为硬币的新标签.(I have also created a new Tag called coin.) 的(The)标签(Tag)元素是另一个可用于标识用户定义的GameObject类型的属性.默认情况下,您只有以下内容(element is another attribute that can be used to identify the user defined type of a GameObject. By default, you will only have the following)标签(Tags):(:)
-
未标记(Untagged)
-
重生(Respawn)
-
完(Finish)
-
仅编辑(EditorOnly)
-
主相机(MainCamera)
-
播放器(Player)
-
游戏控制器(GameController)
要添加新标签,您需要选择(To add a new tag, you will need to select)添加标签…(Add Tag…)然后在列表元素中输入您的标签.在这种情况下,我创建了一个名为(and enter your Tag in the list element. In this case, I created a new tag called)**硬币(coin)**并从更改了Tag属性(and changed the Tag attribute from)**未标记(Untagged)**至(to)**硬币(coin)**在里面(in the)检查器窗口(Inspector Window).(.) 现在,您可以运行该程序以测试新的实现.您的硬币应该在世界上的位置旋转.(Now you can run the program to test the new implementation. Your coin should be rotating in its location as positioned in the world.) 旋转硬币清单:(Listing for rotating coin:)
using UnityEngine;
using System.Collections;
public class rotateCoin : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
this.transform.Rotate (Vector3.right, 3);
}
}
检测碰撞:(Detecting Collision:)
我们游戏的目的是捡硬币.为此,我们需要(The objective of our game is to pick-up coins. In order to do so, we need our)CP(CP)以便能够检测它是否已碰撞/相交硬币对象.为此,我们需要扩展附加到(to be able to detect if it has collided/intersected a coin object or not. To do so, we need to expand the script attached to the)CP(CP)处理此事件.(to handle this event.) 由于这是一款简单的游戏,没有太多的互动和后勤工作,因此我们可以使用处理玩家输入的相同脚本来解决碰撞检测问题.所以我们可以扩展(Since this is a simple game with not too many interactions and logistics, we can use the same script that handles the player’s input to also address the collision detection. So we can expand out)**playerInput.cs(playerInput.cs)**脚本如下:(script to the following:)
using UnityEngine;
using System.Collections;
public class playerInput : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// code for the movement of player (CP) forward
if(Input.GetKey(KeyCode.UpArrow)){
this.transform.Translate(Vector3.forward * Time.deltaTime);
}
// code for the movement of player (CP) backward
if(Input.GetKey(KeyCode.DownArrow)){
this.transform.Translate(Vector3.back * Time.deltaTime);
}
// code for the movement of player (CP) left
if(Input.GetKey(KeyCode.LeftArrow)){
this.transform.Rotate(Vector3.up, -5);
}
// code for the movement of player (CP) right
if(Input.GetKey(KeyCode.RightArrow)){
this.transform.Rotate(Vector3.up, 5);
}
}
// This event will be raised by object that have their Is Trigger attributed enabled.
// In our case, the coin GameObject has Is Trigger set to true on its collider.
void OnTriggerEnter(Collider c){
if(c.tag.Equals("coin")){
Debug.Log("Got the Coin!");
}
}
}
处理触发事件的主要功能是(The main function that handles the trigger event is)OnTriggerEnter(对撞机c)(OnTrigeerEnter(Collider c)).此函数将获取正在进入对象碰撞器的对象的碰撞器(. This function will get the collider of the object that is entering the collider of the)CP(CP)对象.在对撞机中,我们检查与之碰撞的物体的种类,为此,我们正在使用(objet. In the collider, we check to see what kind of an object we have collided with, and to do so, we are using the)标签(Tag)附加到我们正在与之碰撞的GameObject上的元素!(element attached to the GameObject we are colliding with!)
在这种情况下,我们将打印到控制台窗口" Got the Coin!".每次我们与硬币对象的实例碰撞时.(In this case, we are going to print out to the console window “Got the Coin!” each time we collide with an instance of the coin object.)
兴趣点(Points of Interest)
在第5部分中,我们讨论了很多想法和概念.我们简要讨论了如何在开始任何设计和编码之前,从构思想法和构想开始,并在纸上设置游戏规则和界限.我们研究了如何将3D模型导入Unity 3D.我们研究了如何创建一个(We covered a huge amount of ideas and concepts in Part 5. We discussed briefly how to start with developing an idea and a vision and setting game rules and boundaries on paper before we start any designing and coding. We looked at how to import a 3D model into Unity 3D. We looked at how to create a)角色播放器(CP)(Character Player (CP))以及如何对其应用运动.(and how to apply movement to it.) 我们还讨论了如何向给定组件添加组件(We also discussed how to add components to a given)游戏对象(GameObject).例如,对于(. For instance, for the)CP(CP),我们必须添加一个(, we had to add a)刚体(Rigidbody)零件.这使我们能够使用物理引擎开始在世界范围内创建更多逼真的交互.(component. This allows us to use the physics engine to start creating more lifelike interactions within the world.) 我们还看了(We also looked at the)是触发(Is Trigger)中的属性(attribute in the)对撞机(Collider)的组成部分(component of the)硬币(coin)游戏对象.(GameObject.) 总而言之,本文以非常简短和简化的方式介绍了很多信息.这个想法是让您自己更深入地挖掘和熟悉它们.(All in all, there is a lot of information that we covered in the article in a very short and simplified way. The idea is for you to dig deeper and become more familiar with them on your own.) 在下一部分中,我们将继续游戏,开始引入更多的交互作用,并开始为其开发一些GUI(图形用户界面).(In the next part, we will continue with our game and start introducing more interaction and also start developing some GUI (Graphical User Interface) for it.)
历史(History)
这是系列文章的第五篇,我将慢慢为Code Project社区做出贡献.(This is the fifth article of a series which I would slowly contribute to the Code Project community.)
-
Unity 3D –游戏编程–第5部分(Unity 3D – Game Programming – Part 5)
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# .NET Mobile iPhone Android Windows Design Dev 新闻 翻译