unity 3d游戏开发_使用Unity 5开发3D游戏
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飛船游戲。 完成游戲的構建后,它將如下所示:
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游戲的資產包,最后單擊創建項目 。
了解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模型插入游戲。 我在場景中插入了一個立方體。 屏幕現在應如下所示:
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對象相關的許多選項,例如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對象并選擇“ 組件”>“物理”>“剛體”將物理規則應用于任何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:
下圖顯示了攝像機的視野和可以通過單擊攝像機對象打開的攝像機預覽:
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:
您可以通過單擊游戲選項卡旁邊的資產商店選項卡來打開資產商店。 我為游戲選擇了太空飛船,屏幕如下所示:
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)效果很好。 通過單擊“ 游戲”選項卡進入游戲模式,并將“剛體”角色添加到飛船中。 別忘了從右窗格中“ 剛體”選項下的“ 使用重力”選項中刪除勾號(您不想讓太空飛船掉下來,對嗎?)。 屏幕現在應如下所示:
建立游戲環境 (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:
您可以開發一個迷宮,使您的飛船必須避開所有障礙物,并且必須到達端壁并必須觸摸它以避免撞到。 將剛體部件添加到所有部件中。 從頂視圖看,這是我的游戲場:
向對象添加行為 (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按鈕。
#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.
上面的代碼將計算相機對象和LateUpdate()的位置之間的差異,并根據玩家的位置更新相機的位置(使用LateUpdate()函數)。
Now, add a script to the Player object.
現在,將腳本添加到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.
在這里,我們導入了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函數根據作用在剛體上的力使飛船旋轉。 rotation和AddForce方法都使用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:
您的全新太空飛船游戲現已制作完成。 通過單擊文件>將場景另存為...保存場景 。 現在選擇“ 文件”>“構建設置” 。 選擇您的操作系統,然后單擊“ 生成”按鈕。 您的屏幕應如下所示:
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游戲開發
總結
以上是生活随笔為你收集整理的unity 3d游戏开发_使用Unity 5开发3D游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方法模板ThinkPHP3.1.2项目技
- 下一篇: 双亲表示法、孩子表示法、孩子兄弟表示法(