当前位置:   article > 正文

unity 3d游戏开发_使用Unity 5开发3D游戏

基于unity5buildsystem

unity 3d游戏开发

If there’s one thing cooler than playing games, it’s building games.

如果有比玩游戏更酷的一件事,那就是构建游戏。

Whether you’re building 2D games like Angry Birds, or 3D games like Counter Strike, you can earn money by publishing them at places like Play Store, App Store or on the Web.

无论您是构建《愤怒的小鸟》之类的2D游戏,还是《 Counter Strike》之类的3D游戏,都可以通过在Play商店,App Store或网络上发布它们来赚钱。

如何开始制作游戏 (How to Get Started Building Games)

There are many tools for creating games, and in this article, we’ll look at the very popular Unity 5, a gaming platform that facilitates 2D and 3D game development for a variety of platforms such as OS X, Linux, Windows, Unity Webplayer, Android, iOS, BlackBerry 10, Windows Phone, Tizen, WebGL, PlayStation, Wii U, Nintendo, Xbox, Android TV, Samsung Smart TV, Oculus Rift, HTC Vive and Gear VR.

有很多用于创建游戏的工具,在本文中,我们将介绍非常流行的Unity 5 ,该游戏平台可为OS X,Linux,Windows,Unity Webplayer等各种平台促进2D和3D游戏开发,Android,iOS,BlackBerry 10,Windows Phone,Tizen,WebGL,PlayStation,Wii U,Nintendo,Xbox,Android TV,Samsung Smart TV,Oculus Rift,HTC Vive和Gear VR。

In this article, we’ll look at how to build a 3D spaceship game full of obstacles, enemies, maneuvering and shooting. After we finish building our game, it will look like this:

在本文中,我们将研究如何构建一个充满障碍,敌人,机动和射击的3D飞船游戏。 完成游戏的构建后,它将如下所示:

Spaceship Game Final

We’ll walk through the installation of Unity3D Personal Edition, develop a 3D game employing 3D modeling, Unity’s resourceful asset store, scripting (JavaScript) and Unity’s powerful physics engine.

我们将逐步完成Unity3D Personal Edition的安装,使用3D建模,Unity的资源丰富的资产存储,脚本(JavaScript)和Unity的强大物理引擎来开发3D游戏。

There are no pre-requisites for following this tutorial, although a basic knowledge of JavaScript would to be helpful.

尽管有JavaScript的基本知识会有所帮助,但没有先决条件。

安装 (Installation)

Installing Unity is as easy as downloading the installer for your respective platform from here.

这里下载适用于您各自平台的安装程序就像安装Unity一样容易。

The download manager is a small file that prompts you to select which components you can download. These downloads are typically large in size (in gigabytes), so you might want to sit back or read through this tutorial before your installation process is complete.

下载管理器是一个小文件,提示您选择可以下载的组件。 这些下载文件通常很大(以GB为单位),因此您可能需要坐下来或通读本教程,然后再完成安装过程。

After installation, open Unity 5 and select New Project under the Create a Project option. Enter the name of the project and select the 3D option to import the asset packages for 3D games, and finally click on Create Project.

安装后,打开Unity 5,然后在“创建项目”选项下选择“ 新建项目 ”。 输入项目的名称,然后选择3D选项以导入3D游戏的资产包,最后单击创建项目

Create Project

了解3D模型和相机 (Understanding 3D Models and the Camera)

If you’ve followed all the steps in the Installation section successfully, you’ll get the Unity 5 interface for creating games. Since we’re building a 3D game, we’ll need to have 3D objects in our gameplay. You can insert a 3D model into the game by selecting GameObject > 3D Object. I inserted a cube into the scene. Your screen should now look like this:

如果成功地遵循了“安装”部分中的所有步骤,则将获得用于创建游戏的Unity 5界面。 由于我们正在构建3D游戏,因此我们需要在游戏玩法中包含3D对象。 您可以通过选择GameObject> 3D Object将3D模型插入游戏。 我在场景中插入了一个立方体。 屏幕现在应如下所示:

3D Models

Upon clicking on the cube, you’ll see a number of options related to the 3D object you’ve selected – such as Transform, Mesh, Box Collider and Mesh Renderer.

单击多维数据集后,您将看到与所选3D对象相关的许多选项,例如TransformMeshBox ColliderMesh Renderer

These 3D models interact with each other and the gameplay using what’s called a Physics Engine. You can apply physics rules to any 3D object by clicking on it and selecting Components > Physics > Rigidbody.

这些3D模型使用称为Physics Engine物理引擎彼此交互,并与游戏玩法互动。 您可以通过单击任意3D对象并选择“ 组件”>“物理”>“刚体”将物理规则应用于任何3D对象。

This will cause Unity’s gaming engine to treat the object as a real-word Rigid Body and apply various real-world interactions to it, such as collisions and gravity.

这将导致Unity的游戏引擎将对象视为真实单词的刚体,并对其施加各种现实世界的交互作用,例如碰撞和重力。

Unity uses cameras to render the objects in the game space. When you enter the Game mode to have a look at your game field, you’ll be able to view it from the camera’s viewpoint – which is also what players of the game will see.

Unity使用相机在游戏空间中渲染对象。 当您进入“ 游戏”模式以查看您的游戏场时,您将能够从相机的角度查看它-这也是游戏玩家所看到的。

The following image shows the field of view of the camera and the camera preview that can be opened by clicking on the camera object:

下图显示了摄像机的视野和可以通过单击摄像机对象打开的摄像机预览:

Camera

It’s worth playing around a bit with 3D models and cameras, as the more familiar you are with them, the better you’ll understand the steps that follow.

值得一试的是使用3D模型和相机,因为您对它们越熟悉,就越能理解后续步骤。

使用Unity Asset Store (Using Unity Asset Store)

Since we’ve just started, it’s difficult for beginners to develop very complicated players as well as scenery for the game. Unity’s asset store comes to the rescue to help us solve this problem.

由于我们才刚刚开始,因此对于初学者来说,很难开发出非常复杂的玩家以及游戏场景。 Unity的资产存储库将竭尽全力帮助我们解决此问题。

Unity’s asset store is a collection of various models, materials, sounds, animations and more. The assets can be of many types and many models may have animations and sounds attached with them for making your work easier.

Unity的资产存储库是各种模型,材料,声音,动画等的集合。 这些素材资源可以具有多种类型,并且许多模型可能附带有动画和声音,以使您的工作变得更轻松。

Just like any other store, there are free as well as paid assets available for building a game. Since we’re building a spaceship game, let’s get a cool looking spaceship for our game.

就像其他商店一样,免费和付费资产可用于制作游戏。 由于我们正在构建太空飞船游戏,因此让我们为游戏制作一款外观酷酷的太空飞船。

You can open the Asset Store by clicking on the Asset Store tab beside the Game tab. I chose a spaceship set for the game and my screen looked like this:

您可以通过单击游戏选项卡旁边的资产商店选项卡来打开资产商店。 我为游戏选择了太空飞船,屏幕如下所示:

Asset Store

You can also download this asset set by clicking here. Now click on Import to import this asset set to your project. You can bring the spaceship to the gameplay by clicking on omega_fighter_01 > model and dragging omega_fighter to the Hierarchy pane on the left side of the screen. Your model should now be in the game area.

您也可以通过单击此处下载此资产集。 现在单击“ 导入”以将该资产集导入到您的项目中。 您可以通过单击omega_fighter_01>模型并将omega_fighter拖到屏幕左侧的“层次结构”窗格,将飞船带入游戏玩法。 您的模型现在应该在游戏区域中。

You should now reposition the spaceship at origin (0, 0, 0) by clicking on the spaceship and entering the respective values in the Transform menu on the right pane. You will also need to reposition the camera using the same technique.

现在,您应该通过单击飞船并在右窗格的“ 转换”菜单中输入相应的值,将飞船重新放置在原点(0, 0, 0) 。 您还需要使用相同的技术来重新定位相机。

For me (0, 0.8, -1.5) with a rotation of (25, 0, 0) worked well. Enter the game mode by clicking on the Game tab and add the Rigidbody character to spaceship. Don’t forget to remove the tick from the Use Gravity option under the Rigidbody option in the right pane (you wouldn’t want your spaceship to fall off the sky, would you?). Your screen should now look something like this:

对我来说(0, 0.8, -1.5)旋转(25, 0, 0) (0, 0.8, -1.5) (25, 0, 0)效果很好。 通过单击“ 游戏”选项卡进入游戏模式,并将“刚体”角色添加到飞船中。 别忘了从右窗格中“ 刚体”选项下的“ 使用重力”选项中删除勾号(您不想让太空飞船掉下来,对吗?)。 屏幕现在应如下所示:

Game View

建立游戏环境 (Constructing the Game Setting)

Let’s construct a simple game setting by setting up some obstacles. Place a ground with the help of Components > 3D Objects > Plane options and add some pillars.

让我们通过设置一些障碍来构建一个简单的游戏设置。 借助“ 组件”>“ 3D对象”>“平面”选项放置地面,并添加一些Struts。

You can develop a maze where your spaceship has to avoid all the obstacles and have to reach the ending wall and has to touch it to avoid being hit. Add rigid body component to all of them. Here’s how my game field looks like from the top view:

您可以开发一个迷宫,使您的飞船必须避开所有障碍物,并且必须到达端壁并必须触摸它以避免撞到。 将刚体部件添加到所有部件中。 从顶视图看,这是我的游戏场:

Top View

向对象添加行为 (Adding Behavior to Objects)

Your game now needs logic to run. You’ll have to program how the spaceship moves, what happens when the spaceship collides and so on.

您的游戏现在需要运行逻辑。 您将必须编程太空船如何移动,太空船碰撞时会发生什么等等。

CameraController (CameraController)

You’ll have to program your Main Camera to move along with your player, otherwise you won’t be able to keep track of your spaceship once it moves out of the camera’s field of view.

您必须对主摄像机进行编程以使其与播放器一起移动,否则一旦飞船移出摄像机视野,您将无法对其进行跟踪。

From the menu bar, select Assets > Create > Javascript to create a JavaScript file. Now click on Main Camera and select the Add Component > Scripts > CameraController button from the bottom of the right pane.

在菜单栏中,选择资产>创建> Javascript以创建一个JavaScript文件。 现在,单击Main Camera ,然后从右窗格的底部选择Add Component> Scripts> CameraController按钮。

  1. #pragma strict
  2. // variable offset of the type Vector3
  3. var offset : Vector3;
  4. // variable player of the type GameObject
  5. var player : GameObject;
  6. function Start () {
  7. offset = transform.position - player.transform.position;
  8. }
  9. function LateUpdate () {
  10. transform.position = player.transform.position + offset;
  11. }

The above code will calculate the difference between the positions of the camera object and the GameObject and will update the camera’s position (using the LateUpdate() function) according to the player’s position.

上面的代码将计算相机对象和LateUpdate()的位置之间的差异,并根据玩家的位置更新相机的位置(使用LateUpdate()函数)。

Now, add a script to the Player object.

现在,将脚本添加到Player对象。

  1. #pragma strict
  2. import UnityEngine.UI;
  3. var loseText : Text;
  4. var winText : Text;
  5. var rb : Rigidbody;
  6. var speed : float;
  7. var tilt : float;
  8. // initialising variables
  9. function Start () {
  10. rb = GetComponent.<Rigidbody> ();
  11. loseText.text = "";
  12. winText.text = "";
  13. }
  14. function Update () {
  15. function FixedUpdate () {
  16. // take input from keyboard
  17. var moveHorizontal : float = Input.GetAxis ("Horizontal");
  18. var moveVertical : float = Input.GetAxis ("Vertical");
  19. // map the movement with Vector3
  20. var movement : Vector3 = new Vector3 (moveHorizontal, 0.0f, moveVertical);
  21. // add force to the object
  22. rb.AddForce (movement * speed);
  23. // add rotation to the object
  24. rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
  25. }
  26. // works when two objects collide with each other
  27. function OnTriggerEnter(other : Collider) {
  28. if (other.tag == "Obstacles") {
  29. Destroy(gameObject);
  30. Destroy(other.gameObject);
  31. loseText.text = "You Lose!";
  32. }
  33. if (other.tag == "Win") {
  34. Destroy(gameObject);
  35. Destroy(other.gameObject);
  36. winText.text = "You Win!";
  37. }
  38. }

Here we have imported UnityEngine.UI to import the datatype Text, which is present in the UI toolkit of Unity. The above code maps the movements of the spaceship (player object) with the arrow keys (using the Input.GetAxis function), and moves it using the AddForce function.

在这里,我们导入了UnityEngine.UI来导入数据类型Text ,该数据类型存在于Unity的UI工具包中。 上面的代码使用箭头键(使用Input.GetAxis函数)映射了太空飞船(玩家对象)的运动,并使用AddForce函数进行了移动。

It also introduces a tilt to the spaceship as the spaceship moves left or right. The Quaternion.Euler function is used for the rotation of the spaceship depending on the force acting on the Rigidbody. Both the rotation and AddForce methods make use of a Vector3 object for taking information on the direction where the movement should take place.

当飞船向左或向右移动时,也会给飞船带来倾斜。 Quaternion.Euler函数根据作用在刚体上的力使飞船旋转。 rotationAddForce方法都使用Vector3对象来获取有关移动方向的信息。

We have introduced logic that if the player touches an object with the tag Obstacles, the Destroy function destroys the spaceship and the object with which it collides and then You Lose! text is displayed. If it touches an object with the tag Win, it displays You Win!. The latter object is the last wall of the maze.

我们引入了一种逻辑,即如果玩家触摸带有标签Obstacles的物体,则Destroy功能会破坏飞船及其与之碰撞的物体,然后您就迷失! 显示文字。 如果触摸带有Win标签的对象,则显示You Win! 。 后一个对象是迷宫的最后一堵墙。

Now, going back to the game window, you’ll notice that the scripts have been attached to the respective objects.

现在,回到游戏窗口,您会注意到脚本已附加到各个对象。

You’ll now need to create text objects, which is possible by selecting GameObject > UI > Text. Position the text accordingly. Now click on the Player Object and have a look at the settings under the script section.

现在,您需要创建文本对象,这可以通过选择GameObject> UI> Text来实现 。 相应地放置文本。 现在,单击“ 播放器对象”,然后查看脚本部分下的设置。

There should be options like Lose Text and Win Text. Assign the empty fields the appropriate objects by dragging the right game objects from under the hierarchy in the left pane to the empty fields. Setting Speed 8.5 and Tilt 4 worked well for me. Similarly, add appropriate options to the Camera object. You can now test the game by clicking on the Play icon on the top (center) of the window.

应该有诸如“ 丢失文本”和“ 赢得文本”之类的选项。 通过将正确的游戏对象从左窗格中的层次结构下方拖动到空白字段,为空白字段分配适当的对象。 设置Speed 8.5和Tilt 4对我来说效果很好。 同样,将适当的选项添加到Camera对象。 现在,您可以通过单击窗口顶部(中间)的“ 播放”图标来测试游戏。

编译和构建独立 (Compiling and Building a Standalone)

Your brand new spaceship game is now constructed. Save the scene by clicking on File > Save Scene as…. Now select File > Build Settings. Select your operating system and click on the Build button. Your screen should look like this:

您的全新太空飞船游戏现已制作完成。 通过单击文件>将场景另存为...保存场景 。 现在选择“ 文件”>“构建设置” 。 选择您的操作系统,然后单击“ 生成”按钮。 您的屏幕应如下所示:

Building Standalone

This will create a standalone file for your operating system. Your game is now ready to be played and distributed. Here’s a video of the gameplay:

这将为您的操作系统创建一个独立文件。 您的游戏现在可以播放和分发了。 这是游戏玩法的视频:

As you see at the end of the video, I put a huge helicopter in the game – from the Asset Store – and added the Win tag to it. In this way, if my spaceship reaches the helicopter, the game ends – and you’re a winner!

正如您在视频结尾看到的那样,我在资源商店中将一架大型直升机投入了游戏,并在其中添加了“ Win标签。 这样,如果我的飞船到达直升机,游戏就结束了-你就是赢家!

结论 (Conclusion)

Unity 5 provides a great opportunity for interested people to develop 2D as well as 3D games. As we’ve seen, the game development workflow – including the scripting – is quite elementary, and you can easily develop games of varying complexity using the same workflow.

Unity 5为感兴趣的人提供了一个很好的机会来开发2D和3D游戏。 如我们所见,游戏开发工作流程(包括脚本)非常基础,您可以使用同一工作流程轻松开发复杂程度不同的游戏。

An example of a game that you could publish and sell would be a spaceship game where enemies fire at you and you fire back at them. The game might consist of several levels or stages.

您可以发布和出售的游戏的一个例子是一艘飞船游戏,敌人向您开枪,而您又向他们开枪。 游戏可能包含几个级别或阶段。

The learning curve for developing professional games for Unity is not very steep. The Unity Asset Store is a great resource for finding interesting assets for your game, so that you don’t have to start working on your new game from scratch.

为Unity开发专业游戏的学习曲线不是很陡。 Unity资产商店是为您的游戏寻找有趣资产的绝佳资源,因此您不必从头开始开发新游戏。

Have you developed any games with the help of Unity? What is your favorite gaming engine?

您是否在Unity的帮助下开发了任何游戏? 您最喜欢的游戏引擎是什么?

翻译自: https://www.sitepoint.com/developing-3d-games-with-unity-5/

unity 3d游戏开发

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号