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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

制作一款3D炸弹超人游戏

發布時間:2025/3/8 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 制作一款3D炸弹超人游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

說起炸彈超人,相信很多朋友都玩過類似的游戲,其中最為人熟知的莫過于《泡泡堂》。該類型游戲需要玩家在地圖中一邊跑動一邊放置炸彈,同時還要躲避敵方炸彈保護自己。最初的炸彈超人游戲都是2D的,今天這篇文章將教大家在Unity中實現一款3D的炸彈超人游戲。

準備工作

將項目初始資源導入Unity項目,資源目錄如下:



其中分別包含要用于游戲的動畫、材質、模型、背景音樂、物理材質、預制件、場景、腳本、音效及圖片資源。

放置炸彈

打開項目中的Game場景并運行。



可以通過WASD鍵或方向鍵來操作所有角色進行移動。下面來讓角色可以放置炸彈。角色1(紅色)通過按下空格鍵來放置炸彈,角色2(藍色)則通過按下回車鍵進行同樣的操作。

打開Player腳本,該腳本負責角色所有的移動及動畫邏輯。找到DropBomb函數,添加代碼如下:

1 /// <summary> 2 /// Drops a bomb beneath the player 3 /// </summary> 4 private void DropBomb() { 5 if (bombPrefab) { //Check if bomb prefab is assigned first 6 // Create new bomb and snap it to a tile 7 Instantiate(bombPrefab, 8 new Vector3(Mathf.RoundToInt(myTransform.position.x), bombPrefab.transform.position.y, Mathf.RoundToInt(myTransform.position.z)), 9 bombPrefab.transform.rotation); 10 } 11 }

其中RoundToInt函數用于對炸彈的坐標參數四舍五入,以避免炸彈放置位置偏離出地塊中心。



運行場景,效果如下:



創建爆炸效果

在Scripts文件夾下新建C#腳本命名為Bomb:



找到Prefabs文件夾下的Bomb預制件,將Bomb腳本綁定到該游戲對象上。然后打開Bomb腳本,添加代碼如下:

1 using UnityEngine; 2 using System.Collections; 3 using System.Runtime.CompilerServices; 4 5 public class Bomb : MonoBehaviour { 6 public AudioClip explosionSound; 7 public GameObject explosionPrefab; 8 public LayerMask levelMask; // This LayerMask makes sure the rays cast to check for free spaces only hits the blocks in the level 9 private bool exploded = false; 10 11 // Use this for initialization 12 void Start() { 13 Invoke("Explode", 3f); //Call Explode in 3 seconds 14 } 15 16 void Explode() { 17 //Explosion sound 18 AudioSource.PlayClipAtPoint(explosionSound,transform.position); 19 20 //Create a first explosion at the bomb position 21 Instantiate(explosionPrefab, transform.position, Quaternion.identity); 22 23 //For every direction, start a chain of explosions 24 StartCoroutine(CreateExplosions(Vector3.forward)); 25 StartCoroutine(CreateExplosions(Vector3.right)); 26 StartCoroutine(CreateExplosions(Vector3.back)); 27 StartCoroutine(CreateExplosions(Vector3.left)); 28 29 GetComponent<MeshRenderer>().enabled = false; //Disable mesh 30 exploded = true; 31 transform.FindChild("Collider").gameObject.SetActive(false); //Disable the collider 32 Destroy(gameObject,.3f); //Destroy the actual bomb in 0.3 seconds, after all coroutines have finished 33 } 34 35 public void OnTriggerEnter(Collider other) { 36 if (!exploded && other.CompareTag("Explosion")) { //If not exploded yet and this bomb is hit by an explosion... 37 CancelInvoke("Explode"); //Cancel the already called Explode, else the bomb might explode twice 38 Explode(); //Finally, explode! 39 } 40 } 41 42 private IEnumerator CreateExplosions(Vector3 direction) { 43 for (int i = 1; i < 3; i++) { //The 3 here dictates how far the raycasts will check, in this case 3 tiles far 44 RaycastHit hit; //Holds all information about what the raycast hits 45 46 Physics.Raycast(transform.position + new Vector3(0,.5f,0), direction, out hit, i, levelMask); //Raycast in the specified direction at i distance, because of the layer mask it'll only hit blocks, not players or bombs 47 48 if (!hit.collider) { // Free space, make a new explosion 49 Instantiate(explosionPrefab, transform.position + (i * direction), explosionPrefab.transform.rotation); 50 } 51 else { //Hit a block, stop spawning in this direction 52 break; 53 } 54 55 yield return new WaitForSeconds(.05f); //Wait 50 milliseconds before checking the next location 56 } 57 58 } 59 }

在檢視面板中,將Bomb預制件賦值給腳本的Explosion Prefab屬性,該屬性用于定義需要生成爆炸效果的對象。Bomb腳本使用了協程來實現爆炸的效果,StartCoroutine函數將朝著4個方向調用CreateExplosions函數,該函數用于生成爆炸效果,在For循環內遍歷炸彈能夠炸到的所有單元,然后為能夠被炸彈影響的各個單元生成爆炸特效,炸彈對墻壁是沒有傷害的。最后,在進入下一次循環前等待0.05秒。

代碼作用類似下圖:



紅線就是Raycast,它會檢測炸彈周圍的單元是否為空,如果是,則朝著該方向生成爆炸效果。如果碰撞到墻,則不生成爆炸并停止檢測該方向。所以前面需要讓炸彈在地塊中心生成,負責就會出現不太理想的效果:



Bomb代碼中定義的LayerMask用于剔除射線對地塊的檢測,這里還需要在檢視面板中編輯層,并新增用戶層命名為“Blocks”,然后將層級視圖中Blocks游戲對象的Layer設置為“Blocks”。



更改Blocks對象的層級時會跳出提示框,詢問是否更改子節點,選擇是即可:



然后選中Bomb對象,在檢視面板中將Bomb腳本的Level Mask設為“Blocks”:



連鎖反應

如果炸彈炸到了另一個炸彈,那么被炸到的炸彈也會爆炸。Bomb腳本中的OnTriggerEnter
函數是MonoBehaviour預定義的函數,會在觸發器與Rigidbody碰撞之前調用。這里OnTriggerEnter會檢測被碰撞的炸彈是否是被炸彈特效所碰撞,如果是,則該炸彈也要爆炸。

現在運行場景,效果如下:



判定游戲結果

打開Player腳本,添加下面的代碼:

1 //Manager 2 public GlobalStateManager GlobalManager; 3 4 //Player parameters 5 [Range(1, 2)] //Enables a nifty slider in the editor 6 public int playerNumber = 1; //Indicates what player this is: P1 or P2 7 public float moveSpeed = 5f; 8 public bool canDropBombs = true; //Can the player drop bombs? 9 public bool canMove = true; //Can the player move? 10 public bool dead = false; //Is this player dead?

其中GlobalManager是GlobalStateManager腳本的引用,該腳本用于通知玩家獲勝或死亡的消息。dead則用于標志玩家是否死亡。

更改OnTriggerEnter函數代碼如下:

1 public void OnTriggerEnter(Collider other) { 2 if (!dead && other.CompareTag("Explosion")) { //Not dead & hit by explosion 3 Debug.Log("P" + playerNumber + " hit by explosion!"); 4 5 dead = true; 6 GlobalManager.PlayerDied(playerNumber); //Notify global state manager that this player died 7 Destroy(gameObject); 8 } 9 }

該函數作用為設置dead變量來通知玩家死亡,并告知全局狀態管理器玩家的死亡信息,然后銷毀玩家對象。

在檢視面板中選中兩個玩家對象,將Global State Manager游戲對象賦值給Player腳本的Global Manger字段。



再次運行場景,效果如下:



打開GlobalStateManager腳本,添加以下代碼:

1 public List<GameObject> Players = new List<GameObject>(); 2 3 private int deadPlayers = 0; 4 private int deadPlayerNumber = -1; 5 6 public void PlayerDied(int playerNumber) { 7 deadPlayers++; 8 9 if (deadPlayers == 1) { 10 deadPlayerNumber = playerNumber; 11 Invoke("CheckPlayersDeath", .3f); 12 } 13 }

以上代碼用于判斷哪位玩家獲得勝利,如果兩位玩家均死亡,則打成平局。

運行場景,效果如下:



總結

到此本篇教程就結束了,大家還可以在此基礎上對該項目進行擴展,例如添加“推箱子”功能,將位于自己腳邊的炸彈推給敵方,或是限制能夠放置的炸彈數量,添加快速重新開始游戲的界面,設置可以被炸彈炸毀的障礙物,設置一些道具用于獲得炸彈或者增加生命值,還可以增加多人對戰模式與朋友一起變身炸彈超人等等。大家都來發揮自己的創意吧!

文章來自:http://forum.china.unity3d.com/thread-24404-1-1.html

原文鏈接:https://www.raywenderlich.com/125559/make-game-like-bomberman
原作者:Eric Van de Kerckhove

總結

以上是生活随笔為你收集整理的制作一款3D炸弹超人游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

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