Section 39: BobmerMan How-To 1. Getting Started 1. Download, unzip, and open the Starter files 2. Test it out 2. Dropping Bombs 1. Edit the script file Player.cs 1. Edit the method DropBomb(), inside the existing IF statement add: 1. Instantiate(bombPrefab, mytransform.position, bombprefab.transform.rotation); 2. Instantiate creates an instance of a PreFab in code, as opposed to dragging and dropping prefabs in the editor. 2. Test it out. 3. Snapping 1. Edit the script file Player.cs 1. Edit the method DropBomb(), the Instantiate call: 1. Instantiate(bombPrefab, new Vector3(Mathf.RoundToInt(myTransform.position.x), bombprefab.transform.position.y, Mathf.RoundToInt(myTransform.position.z)), bombprefab.transform.rotation); 2. We round off any floats (decimals) to round numbers, creating a grid bombs can drop on. This grid aligns with walls, corners etc. 2. Test it out. 4. Adding Explosions 1. Create a new script, Bomb 2. Assets > Prefabs > Bomb > Add Component > Bomb.cs 3. Edit the script Bomb.cs 1. Add the following variable to the class: 1. public GameObject explosionprefab; 2. Inside the Start() method, add: 1. Invoke( Explode, 3f); 2. Invoke triggers a method. Invoke(<methodName>, <delayinseconds>);
3. Save and Return to Unity. 4. Assets > Prefabs > Bomb > Inspector > Explosion Prefab = Prefabs > Explosion 5. Add the following method: 1. void Explode() Instantiate(explosionPrefab, transform.position, Quaternion.identity); GetComponent<MeshRenderer>().enabled = false; transform.findchild( Collider ).gameobject. SetActive(false); Destroy(gameObject, 0.3f); 1. Spawns an explosion at the bomb location 2. Make bomb invisible 3. Disable colliders so players can walk through explosions 4. Destroy bomb after 0.3 seconds, giving time for explosions to spawn 6. Save, return to Unity, test it out 5. BIGGER Explosions!!! 1. Edit the script Bomb.cs 1. Add the following method: 1. private IEnumerator CreateExplosions(Vector3 direction) return null; //placeholder for now 2. In Explode(), just AFTER Instantiate(), add: 1. StartCoroutine(CreateExplosions(Vector3.forward)); 2. StartCoroutine(CreateExplosions(Vector3.right)); 3. StartCoroutine(CreateExplosions(Vector3.back)); 4. StartCoroutine(CreateExplosions(Vector3.left)); 3. Coroutines essentially pause execution to do something else, then return to executing. 4. In CreateExplosions(), remove the return line, replace with:
1. for(int i = 1; i < 3; i++) RaycastHit hit; Physics.Raycast(transform.position + new Vector3(0, 0.5f, 0), direction, out hit, i, levelmask); if(!hit.collider) Instantiate(explosionPrefab, transform.position + (i * direction), explosionprefab.transform.rotation); else break; yield return new WaitForSeconds(0.05f); 2. Raycast, if not hitting a wall then create an explosion, if hitting wall then don't create explosion, wait a very short time before creating the next explosion in line, giving the illusion the explosion is spreading. 3. Create the class variable: 1. public LayerMask levelmask; 5. Save and return to Unity 2. Unity > top-right > Layers > Edit Layers 1. Layer 8 = Blocks 3. Hierarchy > Map > Blocks 1. Layer = Blocks 2. Yes, change children. 4. Assets > Prefabs > Bomb > Inspector > Level Mask = Blocks 5. Save and test 6. Chain Reactions in Explosions 1. Edit the script Bomb.cs 1. Add the variable: 1. private bool exploded = false; 2. Add the method:
1. public void OnTriggerEnter(Collider other) if(!exploded && other.comparetag( Explosion )) CancelInvoke( Explode ); Explode(); 2. Explodes the bomb but cancels any other invocations of Explode on the same bomb. 3. In Explode(), AFTER GetComponent<MeshRenderer>... 1. exploded = true; 4. Save, return to Unity, test 7. Player Death 1. Edit the script Player.cs 1. Add the variables: 1. public bool dead = false; 2. public GlobalStateManager GlobalManager; 2. In OnTriggerEnter(), UNDER Debug.Log, add: 1. dead = true; 2. GlobalManager.PlayerDied(playerNumber); 3. Destroy(gameObject); 3. Save and return to Unity 2. Hierarchy > select BOTH Player objects > Inspector > Global Manager = Hierarchy > Global State Manager 3. Save and test 8. Ending the Game 1. Edit the script GlobalStateManager.cs 1. Add the variables: 1. private int deadplayers = 0; 2. private int deadplayernumber = -1; 2. In PlayerDied(), add: 1. deadplayers++;
2. if(deadplayers == 1) deadplayernumber = playernumber; Invoke( CheckPlayersDeath, 0.3f); 3. There is a delay in the check to allow for draws 3. Add the using statement: 1. using UnityEngine.SceneManagement; 4. Create a new method: 1. void CheckPlayersDeath() if(deadplayers == 1) if (deadplayernumber == 1) Debug.Log( Player 2 is the winner! ); else Debug.Log( Player 1 is the winner! ); else Debug.Log( The game ended in a draw! ); SceneManager.LoadScene( Game ); 5. Save, return to Unity, and Play!