unity 3d游戏开发_使用Unity 5开发3D游戏
unity 3d游戲開發(fā)
If there’s one thing cooler than playing games, it’s building games.
如果有比玩游戲更酷的一件事,那就是構(gòu)建游戲。
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.
無論您是構(gòu)建《憤怒的小鳥》之類的2D游戲,還是《 Counter Strike》之類的3D游戲,都可以通過在Play商店,App Store或網(wǎng)絡(luò)上發(fā)布它們來賺錢。
如何開始制作游戲 (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.
有很多用于創(chuàng)建游戲的工具,在本文中,我們將介紹非常流行的Unity 5 ,該游戲平臺可為OS X,Linux,Windows,Unity Webplayer等各種平臺促進(jìn)2D和3D游戲開發(fā),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:
在本文中,我們將研究如何構(gòu)建一個(gè)充滿障礙,敵人,機(jī)動和射擊的3D飛船游戲。 完成游戲的構(gòu)建后,它將如下所示:
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的資源豐富的資產(chǎn)存儲,腳本(JavaScript)和Unity的強(qiáng)大物理引擎來開發(fā)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.
下載管理器是一個(gè)小文件,提示您選擇可以下載的組件。 這些下載文件通常很大(以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,然后在“創(chuàng)建項(xiàng)目”選項(xiàng)下選擇“ 新建項(xiàng)目 ”。 輸入項(xiàng)目的名稱,然后選擇3D選項(xiàng)以導(dǎo)入3D游戲的資產(chǎn)包,最后單擊創(chuàng)建項(xiàng)目 。
了解3D模型和相機(jī) (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:
如果成功地遵循了“安裝”部分中的所有步驟,則將獲得用于創(chuàng)建游戲的Unity 5界面。 由于我們正在構(gòu)建3D游戲,因此我們需要在游戲玩法中包含3D對象。 您可以通過選擇GameObject> 3D Object將3D模型插入游戲。 我在場景中插入了一個(gè)立方體。 屏幕現(xiàn)在應(yīng)如下所示:
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.
單擊多維數(shù)據(jù)集后,您將看到與所選3D對象相關(guān)的許多選項(xiàng),例如Transform , Mesh , Box Collider和Mesh 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對象并選擇“ 組件”>“物理”>“剛體”將物理規(guī)則應(yīng)用于任何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.
這將導(dǎo)致Unity的游戲引擎將對象視為真實(shí)單詞的剛體,并對其施加各種現(xiàn)實(shí)世界的交互作用,例如碰撞和重力。
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使用相機(jī)在游戲空間中渲染對象。 當(dāng)您進(jìn)入“ 游戲”模式以查看您的游戲場時(shí),您將能夠從相機(jī)的角度查看它-這也是游戲玩家所看到的。
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:
下圖顯示了攝像機(jī)的視野和可以通過單擊攝像機(jī)對象打開的攝像機(jī)預(yù)覽:
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模型和相機(jī),因?yàn)槟鷮λ鼈冊绞煜?#xff0c;就越能理解后續(xù)步驟。
使用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.
由于我們才剛剛開始,因此對于初學(xué)者來說,很難開發(fā)出非常復(fù)雜的玩家以及游戲場景。 Unity的資產(chǎn)存儲庫將竭盡全力幫助我們解決此問題。
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的資產(chǎn)存儲庫是各種模型,材料,聲音,動畫等的集合。 這些素材資源可以具有多種類型,并且許多模型可能附帶有動畫和聲音,以使您的工作變得更輕松。
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.
就像其他商店一樣,免費(fèi)和付費(fèi)資產(chǎn)可用于制作游戲。 由于我們正在構(gòu)建太空飛船游戲,因此讓我們?yōu)橛螒蛑谱饕豢钔庥^酷酷的太空飛船。
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:
您可以通過單擊游戲選項(xiàng)卡旁邊的資產(chǎn)商店選項(xiàng)卡來打開資產(chǎn)商店。 我為游戲選擇了太空飛船,屏幕如下所示:
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.
您也可以通過單擊此處下載此資產(chǎn)集。 現(xiàn)在單擊“ 導(dǎo)入”以將該資產(chǎn)集導(dǎo)入到您的項(xiàng)目中。 您可以通過單擊omega_fighter_01>模型并將omega_fighter拖到屏幕左側(cè)的“層次結(jié)構(gòu)”窗格,將飛船帶入游戲玩法。 您的模型現(xiàn)在應(yīng)該在游戲區(qū)域中。
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.
現(xiàn)在,您應(yīng)該通過單擊飛船并在右窗格的“ 轉(zhuǎn)換”菜單中輸入相應(yīng)的值,將飛船重新放置在原點(diǎn)(0, 0, 0) 。 您還需要使用相同的技術(shù)來重新定位相機(jī)。
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)旋轉(zhuǎn)(25, 0, 0) (0, 0.8, -1.5) (25, 0, 0)效果很好。 通過單擊“ 游戲”選項(xiàng)卡進(jìn)入游戲模式,并將“剛體”角色添加到飛船中。 別忘了從右窗格中“ 剛體”選項(xiàng)下的“ 使用重力”選項(xiàng)中刪除勾號(您不想讓太空飛船掉下來,對嗎?)。 屏幕現(xiàn)在應(yīng)如下所示:
建立游戲環(huán)境 (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.
讓我們通過設(shè)置一些障礙來構(gòu)建一個(gè)簡單的游戲設(shè)置。 借助“ 組件”>“ 3D對象”>“平面”選項(xiàng)放置地面,并添加一些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:
您可以開發(fā)一個(gè)迷宮,使您的飛船必須避開所有障礙物,并且必須到達(dá)端壁并必須觸摸它以避免撞到。 將剛體部件添加到所有部件中。 從頂視圖看,這是我的游戲場:
向?qū)ο筇砑有袨?(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.
您的游戲現(xiàn)在需要運(yùn)行邏輯。 您將必須編程太空船如何移動,太空船碰撞時(shí)會發(fā)生什么等等。
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.
您必須對主攝像機(jī)進(jìn)行編程以使其與播放器一起移動,否則一旦飛船移出攝像機(jī)視野,您將無法對其進(jìn)行跟蹤。
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.
在菜單欄中,選擇資產(chǎn)>創(chuàng)建> Javascript以創(chuàng)建一個(gè)JavaScript文件。 現(xiàn)在,單擊Main Camera ,然后從右窗格的底部選擇Add Component> Scripts> CameraController按鈕。
#pragma strict// variable offset of the type Vector3 var offset : Vector3;// variable player of the type GameObject var player : GameObject;function Start () {offset = transform.position - player.transform.position; }function LateUpdate () {transform.position = player.transform.position + offset; }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.
上面的代碼將計(jì)算相機(jī)對象和LateUpdate()的位置之間的差異,并根據(jù)玩家的位置更新相機(jī)的位置(使用LateUpdate()函數(shù))。
Now, add a script to the Player object.
現(xiàn)在,將腳本添加到Player對象。
#pragma strict import UnityEngine.UI;var loseText : Text; var winText : Text; var rb : Rigidbody; var speed : float; var tilt : float;// initialising variables function Start () {rb = GetComponent.<Rigidbody> ();loseText.text = "";winText.text = ""; }function Update () {function FixedUpdate () {// take input from keyboardvar moveHorizontal : float = Input.GetAxis ("Horizontal");var moveVertical : float = Input.GetAxis ("Vertical");// map the movement with Vector3var movement : Vector3 = new Vector3 (moveHorizontal, 0.0f, moveVertical);// add force to the objectrb.AddForce (movement * speed);// add rotation to the objectrb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt); }// works when two objects collide with each other function OnTriggerEnter(other : Collider) {if (other.tag == "Obstacles") {Destroy(gameObject);Destroy(other.gameObject);loseText.text = "You Lose!"; }if (other.tag == "Win") {Destroy(gameObject);Destroy(other.gameObject);winText.text = "You Win!";} }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.
在這里,我們導(dǎo)入了UnityEngine.UI來導(dǎo)入數(shù)據(jù)類型Text ,該數(shù)據(jù)類型存在于Unity的UI工具包中。 上面的代碼使用箭頭鍵(使用Input.GetAxis函數(shù))映射了太空飛船(玩家對象)的運(yùn)動,并使用AddForce函數(shù)進(jìn)行了移動。
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.
當(dāng)飛船向左或向右移動時(shí),也會給飛船帶來傾斜。 Quaternion.Euler函數(shù)根據(jù)作用在剛體上的力使飛船旋轉(zhuǎn)。 rotation和AddForce方法都使用Vector3對象來獲取有關(guān)移動方向的信息。
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.
我們引入了一種邏輯,即如果玩家觸摸帶有標(biāo)簽Obstacles的物體,則Destroy功能會破壞飛船及其與之碰撞的物體,然后您就迷失! 顯示文字。 如果觸摸帶有Win標(biāo)簽的對象,則顯示You Win! 。 后一個(gè)對象是迷宮的最后一堵墻。
Now, going back to the game window, you’ll notice that the scripts have been attached to the respective objects.
現(xiàn)在,回到游戲窗口,您會注意到腳本已附加到各個(gè)對象。
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.
現(xiàn)在,您需要創(chuàng)建文本對象,這可以通過選擇GameObject> UI> Text來實(shí)現(xiàn) 。 相應(yīng)地放置文本。 現(xiàn)在,單擊“ 播放器對象”,然后查看腳本部分下的設(shè)置。
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.
應(yīng)該有諸如“ 丟失文本”和“ 贏得文本”之類的選項(xiàng)。 通過將正確的游戲?qū)ο髲淖蟠案裰械膶哟谓Y(jié)構(gòu)下方拖動到空白字段,為空白字段分配適當(dāng)?shù)膶ο蟆?設(shè)置Speed 8.5和Tilt 4對我來說效果很好。 同樣,將適當(dāng)?shù)倪x項(xiàng)添加到Camera對象。 現(xiàn)在,您可以通過單擊窗口頂部(中間)的“ 播放”圖標(biāo)來測試游戲。
編譯和構(gòu)建獨(dú)立 (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:
您的全新太空飛船游戲現(xiàn)已制作完成。 通過單擊文件>將場景另存為...保存場景 。 現(xiàn)在選擇“ 文件”>“構(gòu)建設(shè)置” 。 選擇您的操作系統(tǒng),然后單擊“ 生成”按鈕。 您的屏幕應(yīng)如下所示:
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:
這將為您的操作系統(tǒng)創(chuàng)建一個(gè)獨(dú)立文件。 您的游戲現(xiàn)在可以播放和分發(fā)了。 這是游戲玩法的視頻:
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!
正如您在視頻結(jié)尾看到的那樣,我在資源商店中將一架大型直升機(jī)投入了游戲,并在其中添加了“ Win標(biāo)簽。 這樣,如果我的飛船到達(dá)直升機(jī),游戲就結(jié)束了-你就是贏家!
結(jié)論 (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為感興趣的人提供了一個(gè)很好的機(jī)會來開發(fā)2D和3D游戲。 如我們所見,游戲開發(fā)工作流程(包括腳本)非常基礎(chǔ),您可以使用同一工作流程輕松開發(fā)復(fù)雜程度不同的游戲。
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.
您可以發(fā)布和出售的游戲的一個(gè)例子是一艘飛船游戲,敵人向您開槍,而您又向他們開槍。 游戲可能包含幾個(gè)級別或階段。
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開發(fā)專業(yè)游戲的學(xué)習(xí)曲線不是很陡。 Unity資產(chǎn)商店是為您的游戲?qū)ふ矣腥べY產(chǎn)的絕佳資源,因此您不必從頭開始開發(fā)新游戲。
Have you developed any games with the help of Unity? What is your favorite gaming engine?
您是否在Unity的幫助下開發(fā)了任何游戲? 您最喜歡的游戲引擎是什么?
翻譯自: https://www.sitepoint.com/developing-3d-games-with-unity-5/
unity 3d游戲開發(fā)
總結(jié)
以上是生活随笔為你收集整理的unity 3d游戏开发_使用Unity 5开发3D游戏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方法模板ThinkPHP3.1.2项目技
- 下一篇: 双亲表示法、孩子表示法、孩子兄弟表示法(