1///<summary> 2/// Drops a bomb beneath the player
3///</summary> 4privatevoid DropBomb() {
5if (bombPrefab) { //Check if bomb prefab is assigned first
6// Create new bomb and snap it to a tile 7 Instantiate(bombPrefab,
8new Vector3(Mathf.RoundToInt(myTransform.position.x), bombPrefab.transform.position.y, Mathf.RoundToInt(myTransform.position.z)),
9 bombPrefab.transform.rotation);
10 }
11 }
1using UnityEngine;
2using System.Collections;
3using System.Runtime.CompilerServices;
4 5publicclass Bomb : MonoBehaviour {
6public AudioClip explosionSound;
7public GameObject explosionPrefab;
8public LayerMask levelMask; // This LayerMask makes sure the rays cast to check for free spaces only hits the blocks in the level 9privatebool exploded = false;
1011// Use this for initialization12void Start() {
13 Invoke("Explode", 3f); //Call Explode in 3 seconds14 }
1516void Explode() {
17//Explosion sound18 AudioSource.PlayClipAtPoint(explosionSound,transform.position);
1920//Create a first explosion at the bomb position21 Instantiate(explosionPrefab, transform.position, Quaternion.identity);
2223//For every direction, start a chain of explosions24 StartCoroutine(CreateExplosions(Vector3.forward));
25 StartCoroutine(CreateExplosions(Vector3.right));
26 StartCoroutine(CreateExplosions(Vector3.back));
27 StartCoroutine(CreateExplosions(Vector3.left));
2829 GetComponent<MeshRenderer>().enabled = false; //Disable mesh30 exploded = true;
31 transform.FindChild("Collider").gameObject.SetActive(false); //Disable the collider32 Destroy(gameObject,.3f); //Destroy the actual bomb in 0.3 seconds, after all coroutines have finished33 }
3435publicvoid OnTriggerEnter(Collider other) {
36if (!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 }
4142private IEnumerator CreateExplosions(Vector3 direction) {
43for (int i = 1; i < 3; i++) { //The 3 here dictates how far the raycasts will check, in this case 3 tiles far44 RaycastHit hit; //Holds all information about what the raycast hits4546 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 bombs4748if (!hit.collider) { // Free space, make a new explosion49 Instantiate(explosionPrefab, transform.position + (i * direction), explosionPrefab.transform.rotation);
50 }
51else { //Hit a block, stop spawning in this direction52break;
53 }
5455yieldreturnnew WaitForSeconds(.05f); //Wait 50 milliseconds before checking the next location56 }
5758 }
59 }
1//Manager 2public GlobalStateManager GlobalManager;
3 4//Player parameters 5 [Range(1, 2)] //Enables a nifty slider in the editor 6publicint playerNumber = 1; //Indicates what player this is: P1 or P2 7publicfloat moveSpeed = 5f;
8publicbool canDropBombs = true; //Can the player drop bombs? 9publicbool canMove = true; //Can the player move?10publicbool dead = false; //Is this player dead?
1publicvoid OnTriggerEnter(Collider other) {
2if (!dead && other.CompareTag("Explosion")) { //Not dead & hit by explosion3 Debug.Log("P" + playerNumber + " hit by explosion!");
45 dead = true;
6 GlobalManager.PlayerDied(playerNumber); //Notify global state manager that this player died7 Destroy(gameObject);
8 }
9 }
該函數作用為設置dead變量來通知玩家死亡,并告知全局狀態管理器玩家的死亡信息,然后銷毀玩家對象。
在檢視面板中選中兩個玩家對象,將Global State Manager游戲對象賦值給Player腳本的Global Manger字段。