日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity3d制作2D游戏飞翔的小鸟(FlappyBird)

發布時間:2024/1/8 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity3d制作2D游戏飞翔的小鸟(FlappyBird) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

一、學習方向

二、制作步驟

1.新建項目

3.設置為精靈模式

4.精靈圖像分割

5.場景布局設置

6.錄制小鳥飛翔動畫

7.構建飛翔的小鳥讓其響應

?8.構建UI對象并讓其響應

9.構建游戲背景并讓其移動

10.障礙生成制作

三、結語


一、學習方向

1.學習設置2D圖形

2.搭建2D游戲場景

3.錄制小鳥飛翔動畫

4.構建飛翔的小鳥讓其響應

5.構建UI對象并讓其響應

6.構建游戲背景并讓其移動

7.障礙生成制作

二、制作步驟

1.新建項目

新建unity2D視圖,命名為FlappyBird。(注意:項目名稱和存儲路徑中不要包含中文)

2、導入資源與場景設置

在unity中把我們需要的資源文件Fonts和Sprites拖拽到項目視圖文件夾Assets中如下圖(選中解壓文件夾直接拖動即可)

素材鏈接:https://pan.baidu.com/s/1au5i39kyDubrhRejYtZT9w?pwd=x46m?
提取碼:x46m

3.設置為精靈模式

(1).在工具欄中找到Windows點擊,然后在下拉菜單中找到Package Manager 點擊彈出如下窗口。

(2).在加號后的菜單中選擇Unity? Registry

(3).在Packages選項中找到2D Sprite然后導入(install)即可

4.精靈圖像分割

在Assets文件夾中的sprits文件夾中找到我們的BirdHero選中,然后在Inspector(檢視視圖)中,將Texture Type設置為Sprite(2D and UI),將Sprite Mode設置為Multiple(也就是將單個更改為多個),最后在彈出的窗口中選擇Apply(應用)。(如下圖所示)?

在點擊Apply 之后會彈出新的窗口,在新的窗口最上方菜單欄中選擇Slice下拉菜單中點擊Slice,這樣我們就將BirdHero圖片分成了三個小鳥貼圖。

關閉這個窗口后,在我們的Sprite文件夾的小鳥素材中就可以看到有三個小鳥素材(如果你的依然顯示為BirdHero,不要著急我們只需要點擊后面的小三角即可顯示。)

5.場景布局設置

(1)將BridHero_01拖到Hierarchy(層級視圖)中,重命名為Bird

(2)依次將Grass Thin Sprite和Sky Tile Sprite拖拽到Hierarchy(層級視圖)中重命名為Ground和Background

(3)調整圖層順序

選擇我們需要調整的圖層,在(Inspector)檢視視圖中找到Order in Layer設置數字,將Bird設置為3,Ground設置為1,BackGround設置為0(效果圖如下)

6.錄制小鳥飛翔動畫

(1)給Bird添加組件Polygen Collider 2D和Rigidbody 2D Component

給Ground添加Box Collider 2D,調整屬性,設置參數(如下圖),運行一下會發現小鳥會掉落在草地上。

(2)這時的小鳥還不會飛翔,所以我們需要制作小鳥飛翔的動畫,步驟如下:

a.選擇Bird,在菜單欄中找到Window下拉菜單中的Animation。

b.在彈出的窗口中點擊Creat,接著會彈出新的窗口將文件命名為IDLE用來存放資源,然后點擊保存即可。(如下圖)

c.點擊錄制按鈕,然后點擊Add Property,選擇Sprite Renderer在下拉菜單中找到Sprite,點擊后面的加號。

d.這時候我們需要選中最后一個關鍵幀將其刪除(選中,delete即可)。?

e.選擇第一個關鍵幀,在Inspector(檢視視圖)中將Sprite設置為BirdHero_0?

f.選擇第一個關鍵幀復制,點擊左上角的IDLE下拉菜單中Creat New Clip,命名為Flappy,保存。

在Flappy面板中首先點擊錄制按鈕,然后將我們復制的IDLE的第一個關鍵幀粘貼在我們Flappy中,在Inspector(檢視視圖)中找到Sprite為BirdHero_1。

g.按照步驟f,新建一個Clip命名為DIE,點擊錄制按鈕,復制第一個關鍵幀,然后更改Sprite為BirdHero_2。到這一步我們就完成了小鳥飛翔的錄制。點擊關閉Animation,在彈出的窗口保存為Animations。(如果沒有彈出系統會默認保存在Assets文件中)

(3)雙擊Bird Controller進入Animator 面板中

?選擇IDLE右擊選擇Make Transition

將IDLRE和另外兩個(DIE和Flappy)鏈接起來,然后選擇Flappy右鍵選擇Make Transition制作一條從Flappy到IDLE 的線

在我們的Animator 面板菜單欄中找到Parameters在下面創建兩個Trigger分別命名為Flappy和Die?

選擇從IDLE到DIE的線,在Inspector(檢視視圖)中,取消勾選Has Exit Time,在下面Conditions中點擊加號選擇Die?

選擇從IDLE到Flappy的線,在Inspector(檢視視圖)中,取消勾選Has Exit Time,在下面Conditions中點擊加號選擇Flappy(如下圖所示)。?

從Flappy到IDLE這條線,我們希望在Flappy播放完后直接切換回IDLE,因此不需要取消Has Exit Time的勾選,也不需要設置Conditions,所以這條線就不用管了。

7.構建飛翔的小鳥讓其響應

回到Scene視口,首先我們在Assets中新建一個文件夾:點擊Assets在空白區域右擊選擇Create在下拉列表中找到Folder就會創建出一個新的文件夾,這個文件夾主要存放我們的腳本

雙擊打開我們的Scripts文件夾,然后空白區域右鍵選擇Create下拉列表中的C# Script,然后重命名為Bird,然后將我們的腳本左擊不放拖拽到Bird上面

雙擊打開腳本,進行編譯,這一步是鼠標點擊屏幕小鳥向上飛翔,Ctrl+s保存腳本然后回到我們的Unity中,這時點擊運行我們的游戲,就實現了小鳥向上飛翔

using System.Collections; using System.Collections.Generic; using UnityEngine;public class Bird : MonoBehaviour {public float upperForce = 250f; // the force add to bird when it flappy.private Rigidbody2D rb2d; // the rigidbody 2d componentprivate Animator animator; // the animator componentprivate bool isDead = false; // mark if bird is dead.void Start(){rb2d = GetComponent<Rigidbody2D>();animator = GetComponent<Animator>();}void Update(){if (Input.GetMouseButtonDown(0) && !isDead){ // check input and bird's statusrb2d.velocity = Vector2.zero;rb2d.AddForce(new Vector2(0, upperForce)); // add a upper forceanimator.SetTrigger("Flappy"); // change current animation clip to flappy}}void OnCollisionEnter2D(){rb2d.velocity = Vector2.zero;isDead = true;animator.SetTrigger("Die"); // change current animation clip to Die}}

?8.構建UI對象并讓其響應

(1)在Hierarchy(層級視圖)中右鍵找到UI在下拉列表中找到legacy(舊版)下拉列表選擇Text點擊創建,重命名為Score Text用來記錄分數

(2)選中Score Text,更改Text屬性為Score:0,將字體改為LuckiesGuy,設置字體大小Font Size為22,將Alignment屬性選擇中間兩個將字體居中,把字體顏色更改為白色。

(3)選中Score Text,Ctrl+d拷貝兩份,分別重命名為GameOverText和RestartText,參照上一步調整字體大小及位置(記得更改Text屬性哦),將RestartText拖拽到GameOverText上,讓RestartText成為GameOverText的子對象。效果圖如下

(4)在Hierarchy視圖中右鍵創建空物體CreateEmpty重命名為GameManager

(5)在Scripts文件夾中創建腳本,右鍵>create>C# script,重命名為GameManager,然后將其拖拽到我們創建的空物體GameManager上。

(6)雙擊打開GameManager腳本,進行編輯,主要是更新得分和小鳥死后彈出Game Over UI更新畫面Ctrl+s保存,回到unity

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System;public class GameManager : MonoBehaviour {public static GameManager instance; // single instancepublic GameObject gameOverText; // game over UIpublic Text scoreText; // score textpublic Button playBtn;public GameObject logo;private bool gameOver = false; // mark current game statusprivate int score = 0; // store score.void Awake(){if (instance == null){ // set single instanceinstance = this;}else if (instance != null){Destroy(gameObject);}}void Update(){if (gameOver == true && Input.GetMouseButtonDown(0)){ // if gameover and click the picture, restart the game.SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);}}public void AddScore() // when the bird cross a obstacle, add score.{if (gameOver)return;score += 1;scoreText.text = "Score: " + score.ToString();}public void GameOver() // game over function.{gameOverText.SetActive(true);gameOver = true;}}

(7)雙擊打開Bird腳本,添加GameOver方法GameManager.instance.GameOver(),添加在?void OnCollisionEnter2D中,Ctrl+S保存,回到unity。

void OnCollisionEnter2D(){rb2d.velocity = Vector2.zero;isDead = true;animator.SetTrigger("Die"); // change current animation clip to DieGameManager.instance.GameOver();}

(8)在Hirarchy視圖中添加一個Image(圖片)重命名為logo,和一個Botton(按鈕)重命名為playBtn

選中logo在Inspector視圖中更改Source Image屬性為標題

選中?playBtn,刪除下面自帶的Text(Legacy),在Inspector視圖中更改Source Image屬性為btnPlay

調整playBtn 和logo位置及大小效果圖如下

(9)選中Hierarchy視圖中的GameManager,將Score Text、Game Over、logo 、playBtn 拖拽到Inspector相應的位置。

選中GameOver 取消勾選Active。

(10)創建一個腳本命名為UIManager,將這個腳本拖拽到logo 和playBtn 上

雙擊打開我們的GameManager腳本,在代碼最后面添加Play的定義指令(添加在GameOver后),這樣我們就可以在UIManager腳本中調用Ctrl+s保存

public void GameOver() // game over function.{gameOverText.SetActive(true);gameOver = true;//UIManager.Instance.ShowUI();}internal void Play(){throw new NotImplementedException();}

接下開打開我們的UIManager腳本進行編輯,Ctrl+s保存,將此腳本拖分別拽到playBtn和logo上

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; public class UIManager : MonoBehaviour {public Button playBtn;public GameObject logo;public static UIManager Instance;// Use this for initializationvoid Awake(){Instance = this;}void Start(){playBtn.onClick.AddListener(onPlay);}// Update is called once per framevoid Update(){}private void onPlay(){playBtn.gameObject.SetActive(false);logo.SetActive(false);GameManager.instance.Play();}public void ShowUI(){playBtn.gameObject.SetActive(true);} }

選中logo在Inspector中修改UIManager 屬性,如下圖所示?

9.構建游戲背景并讓其移動

(1)給Ground添加一個Rigidbody??2D的組件,將Body Type屬性更改為Kinematic(因為是通過腳本來移動,而不是用物理系統來完成移動)

在Scripts文件夾中創建新的腳本重命名為ScorllingObject,Ctrl+S將其賦予到Ground上

using System.Collections; using System.Collections.Generic; using UnityEngine;public class ScrollingObject : MonoBehaviour {public float scrollSpeed = -2f; // object scroll speed.private Rigidbody2D rb2d;void Start(){rb2d = GetComponent<Rigidbody2D>();rb2d.velocity = new Vector2(scrollSpeed, 0);}void Update(){if (GameManager.instance.gameOver == true){rb2d.velocity = Vector2.zero;}}}

(2)新建一個c#腳本重命名為RepeatingBackground,打開編輯腳本,Ctrl+s保存腳本將腳本拖拽到Ground上。

using System.Collections; using System.Collections.Generic; using UnityEngine;public class RepeatingBackground : MonoBehaviour {private BoxCollider2D groundCollider;private float groundHorizontalLength; // store background's width.void Start(){groundCollider = GetComponent<BoxCollider2D>();groundHorizontalLength = groundCollider.size.x; // get background's width;}void Update(){if (transform.position.x < -groundHorizontalLength){ // when it move out of screen, reset it's position.RepositionBackground();}}void RepositionBackground(){Vector2 groundOffset = new Vector2(groundHorizontalLength * 2f, 0);transform.position = (Vector2)transform.position + groundOffset; // reset position}}

(3)將Background拖拽到Ground上,讓其成為Ground的一個子對象,新建一個空物體重命名為Bg,將Ground拖拽到Bg上讓他成為Bg的子對象,調整Ground的位置,復制一份Groung,效果圖如下?。

10.障礙生成制作

(1)將ColumnSprite拖拽到Hierarchy中,為其添加一個Box Collider 2D Component組件調整大小修改屬性參數,如下圖所示?

?(2)復制一份ColumnSprite,旋轉,調整位置如下

(3)創建一個空物體命名為Column,將兩個ColumnSprite拖拽到空物體上,給空物體添加RigidBody 2D組件,設置Type為Kinematic,添加一個Box Collider 2D組件,勾選Is Trigger,調整屬性參數,如下圖所示

?(4)將ScrollingObject腳本拖拽到Column上,然后新建腳本命名為ColumnCtrl+s保存,然后將腳本拖拽到Column上。

using System.Collections; using System.Collections.Generic; using UnityEngine;public class Column : MonoBehaviour {void OnTriggerEnter2D(Collider2D other){if (other.GetComponent<Bird>() != null){GameManager.instance.AddScore();}} }

(5)在Project中新建文件夾命名為Prefabds,將空物體Column拖拽到文件夾中讓其成為預制件,這時你會發現Column變成了藍色。?

(6)新建一個腳本命名為ColumnPool,用于管理Column,將此腳本拖拽到GameManager上然后將預制文件夾中的Column拖拽到Column Prefab中如下圖所示。

using System.Collections; using System.Collections.Generic; using UnityEngine;public class ColumnsPool : MonoBehaviour {public GameObject columnPrefab; // column prefabpublic int poolSize = 5; // max size of poolpublic float spawnRate = 4.5f; // spawn ratepublic float columnYMin = -1.45f; // column's min ypublic float columnYMax = 2.8f; // column's max yprivate GameObject[] columns; // column poolprivate Vector2 startSpawnPos = new Vector2(13f, 0f); // origin start spawn position.private float spawnXPosition = 10f; // column's x not change.private float lastSpawnTime = 0; // record the last spawn time.private int currentIndex = 0; // current column's index in poolvoid Start(){columns = new GameObject[poolSize]; // init columns poolfor (int i = 0; i < poolSize; i++){columns[i] = (GameObject)Instantiate(columnPrefab, startSpawnPos, Quaternion.identity);columns[i].SetActive(false);}}void Update(){if (Time.time - spawnRate > lastSpawnTime){ // after spawnRate time, reset a column's positionlastSpawnTime = Time.time;float columnY = Random.Range(columnYMin, columnYMax); // get a random y.columns[currentIndex].transform.position = new Vector2(spawnXPosition, columnY); // reset positioncolumns[currentIndex].SetActive(true); // active columncurrentIndex++; // turn to next column.if (currentIndex >= poolSize){ // check overflowcurrentIndex = 0;}}}}

(7)刪除Hierarachy中的?Column,運行游戲測試一下即可。

(8)當你運行的時候發現,你的游戲界面是這樣的,那么我們只需要修改游戲場景的大小即可

打開我們的游戲視圖,點擊Free Aspect下拉列表找到+號,創建一個320×480場景大小即可?

最后你就會得到這樣的視圖

三、結語

????????如果你想讓你的游戲更加有趣可以為他添加游戲音效等等,那么本次游戲制作就到此結束啦,不要忘了點贊加關注哦~

注意:如果發現本文章有侵權內容,可以隨時聯系作者本人,本人將即使撤回進行修改。

總結

以上是生活随笔為你收集整理的Unity3d制作2D游戏飞翔的小鸟(FlappyBird)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。