├── .gitattributes ├── Chapter05 ├── AIBrain.cs ├── test.cs ├── InventoryManager.cs ├── SelfDestructTimer.cs ├── TitleMenu.cs ├── Bullet.cs ├── CameraShake.cs ├── PickUpItem.cs ├── FollowCam.cs ├── MainGameHUD.cs ├── GroundObjectController.cs ├── VFXHandler.cs ├── HealthModifier.cs └── GameSessionManager.cs ├── Chapter06 ├── AIBrain.cs ├── InventoryManager.cs ├── SelfDestructTimer.cs ├── Springboard.cs ├── TitleMenu.cs ├── Bullet.cs ├── CameraShake.cs ├── PickUpItem.cs ├── PhysicsForceZone.cs ├── ToggleTimer.cs ├── FollowCam.cs ├── MainGameHUD.cs ├── GroundObjectController.cs ├── VFXHandler.cs ├── HealthModifier.cs └── GameSessionManager.cs ├── Chapter07 ├── AIBrain.cs ├── InventoryManager.cs ├── SelfDestructTimer.cs ├── Springboard.cs ├── TitleMenu.cs ├── Bullet.cs ├── CameraShake.cs ├── PickUpItem.cs ├── PhysicsForceZone.cs ├── ToggleTimer.cs ├── FollowCam.cs ├── MainGameHUD.cs ├── GroundObjectController.cs ├── VFXHandler.cs ├── HealthModifier.cs └── GameSessionManager.cs ├── Chapter10 ├── test.cs ├── InventoryManager.cs ├── SelfDestructTimer.cs ├── Springboard.cs ├── TitleMenu.cs ├── Bullet.cs ├── CameraShake.cs ├── SpawnedSoundFX.cs ├── PhysicsForceZone.cs ├── ToggleTimer.cs ├── FollowCam.cs ├── MainGameHUD.cs ├── GroundObjectController.cs ├── VFXHandler.cs ├── PickUpItem.cs └── GameSessionManager.cs ├── Chapter13 ├── FireObject.cs ├── InventoryManager.cs ├── SelfDestructTimer.cs ├── Springboard.cs ├── TitleMenu.cs ├── Bullet.cs ├── CameraShake.cs ├── SpawnedSoundFX.cs ├── PhysicsForceZone.cs ├── ToggleTimer.cs ├── FollowCam.cs ├── MainGameHUD.cs ├── GroundObjectController.cs ├── VFXHandler.cs ├── PickUpItem.cs └── TorchfireBlock.cs ├── Chapter14 ├── FireObject.cs ├── InventoryManager.cs ├── SelfDestructTimer.cs ├── Springboard.cs ├── TitleMenu.cs ├── Bullet.cs ├── CameraShake.cs ├── SpawnedSoundFX.cs ├── PhysicsForceZone.cs ├── ToggleTimer.cs ├── WorldSpacePrompt.cs ├── FollowCam.cs ├── MainGameHUD.cs ├── SwitchSceneOnCollision.cs ├── GroundObjectController.cs ├── VFXHandler.cs ├── PickUpItem.cs └── TorchfireBlock.cs ├── Chapter08 ├── InventoryManager.cs ├── SelfDestructTimer.cs ├── Springboard.cs ├── TitleMenu.cs ├── Bullet.cs ├── CameraShake.cs ├── PickUpItem.cs ├── PhysicsForceZone.cs ├── ToggleTimer.cs ├── FollowCam.cs ├── MainGameHUD.cs ├── GroundObjectController.cs ├── VFXHandler.cs ├── HealthModifier.cs └── GameSessionManager.cs ├── Chapter09 ├── InventoryManager.cs ├── SelfDestructTimer.cs ├── Springboard.cs ├── TitleMenu.cs ├── Bullet.cs ├── CameraShake.cs ├── PhysicsForceZone.cs ├── ToggleTimer.cs ├── FollowCam.cs ├── MainGameHUD.cs ├── GroundObjectController.cs ├── VFXHandler.cs ├── PickUpItem.cs └── GameSessionManager.cs ├── Chapter11 ├── InventoryManager.cs ├── SelfDestructTimer.cs ├── Springboard.cs ├── TitleMenu.cs ├── Bullet.cs ├── CameraShake.cs ├── SpawnedSoundFX.cs ├── PhysicsForceZone.cs ├── ToggleTimer.cs ├── FollowCam.cs ├── MainGameHUD.cs ├── GroundObjectController.cs ├── VFXHandler.cs ├── PickUpItem.cs └── GameSessionManager.cs ├── Chapter12 ├── InventoryManager.cs ├── SelfDestructTimer.cs ├── Springboard.cs ├── TitleMenu.cs ├── Bullet.cs ├── CameraShake.cs ├── SpawnedSoundFX.cs ├── PhysicsForceZone.cs ├── ToggleTimer.cs ├── FollowCam.cs ├── MainGameHUD.cs ├── GroundObjectController.cs ├── VFXHandler.cs ├── PickUpItem.cs └── GameSessionManager.cs ├── Chapter04 ├── TitleMenu.cs ├── Bullet.cs ├── CameraShake.cs ├── PickUpItem.cs ├── MainGameHUD.cs ├── ItemSpawnZone.cs ├── GroundObjectController.cs ├── HealthModifier.cs └── GameSessionManager.cs ├── Chapter03 ├── Bullet.cs ├── CameraShake.cs ├── PickUpItem.cs ├── ItemSpawnZone.cs ├── GroundObjectController.cs ├── GameSessionManager.cs └── HealthModifier.cs ├── Chapter02 ├── PickUpItem.cs ├── ItemSpawnZone.cs └── GroundObjectController.cs ├── LICENSE ├── Chapter01 └── GroundObjectController.cs └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Chapter05/AIBrain.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class AIBrain : MonoBehaviour 6 | { 7 | void Start() 8 | { 9 | 10 | } 11 | 12 | // Update is called once per frame 13 | void Update() 14 | { 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter06/AIBrain.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class AIBrain : MonoBehaviour 6 | { 7 | void Start() 8 | { 9 | 10 | } 11 | 12 | // Update is called once per frame 13 | void Update() 14 | { 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter07/AIBrain.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class AIBrain : MonoBehaviour 6 | { 7 | void Start() 8 | { 9 | 10 | } 11 | 12 | // Update is called once per frame 13 | void Update() 14 | { 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter05/test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class test : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter10/test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class test : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter13/FireObject.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class FireObject : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter14/FireObject.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class FireObject : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter05/InventoryManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class InventoryManager : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter06/InventoryManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class InventoryManager : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter07/InventoryManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class InventoryManager : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter08/InventoryManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class InventoryManager : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter09/InventoryManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class InventoryManager : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter10/InventoryManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class InventoryManager : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter11/InventoryManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class InventoryManager : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter12/InventoryManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class InventoryManager : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter13/InventoryManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class InventoryManager : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter14/InventoryManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class InventoryManager : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter05/SelfDestructTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SelfDestructTimer : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Seconds until this object self-destructs")] 8 | float _countdownTimer = 1.5f; 9 | 10 | void Update() 11 | { 12 | _countdownTimer -= Time.deltaTime; 13 | if (_countdownTimer <= 0) 14 | Destroy(gameObject); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter06/SelfDestructTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SelfDestructTimer : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Seconds until this object self-destructs")] 8 | float _countdownTimer = 1.5f; 9 | 10 | void Update() 11 | { 12 | _countdownTimer -= Time.deltaTime; 13 | if (_countdownTimer <= 0) 14 | Destroy(gameObject); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter07/SelfDestructTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SelfDestructTimer : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Seconds until this object self-destructs")] 8 | float _countdownTimer = 1.5f; 9 | 10 | void Update() 11 | { 12 | _countdownTimer -= Time.deltaTime; 13 | if (_countdownTimer <= 0) 14 | Destroy(gameObject); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter08/SelfDestructTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SelfDestructTimer : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Seconds until this object self-destructs")] 8 | float _countdownTimer = 1.5f; 9 | 10 | void Update() 11 | { 12 | _countdownTimer -= Time.deltaTime; 13 | if (_countdownTimer <= 0) 14 | Destroy(gameObject); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter09/SelfDestructTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SelfDestructTimer : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Seconds until this object self-destructs")] 8 | float _countdownTimer = 1.5f; 9 | 10 | void Update() 11 | { 12 | _countdownTimer -= Time.deltaTime; 13 | if (_countdownTimer <= 0) 14 | Destroy(gameObject); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter10/SelfDestructTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SelfDestructTimer : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Seconds until this object self-destructs")] 8 | float _countdownTimer = 1.5f; 9 | 10 | void Update() 11 | { 12 | _countdownTimer -= Time.deltaTime; 13 | if (_countdownTimer <= 0) 14 | Destroy(gameObject); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter11/SelfDestructTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SelfDestructTimer : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Seconds until this object self-destructs")] 8 | float _countdownTimer = 1.5f; 9 | 10 | void Update() 11 | { 12 | _countdownTimer -= Time.deltaTime; 13 | if (_countdownTimer <= 0) 14 | Destroy(gameObject); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter12/SelfDestructTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SelfDestructTimer : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Seconds until this object self-destructs")] 8 | float _countdownTimer = 1.5f; 9 | 10 | void Update() 11 | { 12 | _countdownTimer -= Time.deltaTime; 13 | if (_countdownTimer <= 0) 14 | Destroy(gameObject); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter13/SelfDestructTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SelfDestructTimer : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Seconds until this object self-destructs")] 8 | float _countdownTimer = 1.5f; 9 | 10 | void Update() 11 | { 12 | _countdownTimer -= Time.deltaTime; 13 | if (_countdownTimer <= 0) 14 | Destroy(gameObject); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter14/SelfDestructTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SelfDestructTimer : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Seconds until this object self-destructs")] 8 | float _countdownTimer = 1.5f; 9 | 10 | void Update() 11 | { 12 | _countdownTimer -= Time.deltaTime; 13 | if (_countdownTimer <= 0) 14 | Destroy(gameObject); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter06/Springboard.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Springboard : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Velocity change on the Y axis.")] 8 | float _upwardsForce = 300f; 9 | 10 | private void OnCollisionEnter(Collision collision) 11 | { 12 | GameObject hitObj = collision.gameObject; 13 | 14 | if (hitObj != null) 15 | { 16 | Rigidbody rb = hitObj.GetComponent(); 17 | rb?.AddForce(0, _upwardsForce, 0); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter07/Springboard.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Springboard : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Velocity change on the Y axis.")] 8 | float _upwardsForce = 300f; 9 | 10 | private void OnCollisionEnter(Collision collision) 11 | { 12 | GameObject hitObj = collision.gameObject; 13 | 14 | if (hitObj != null) 15 | { 16 | Rigidbody rb = hitObj.GetComponent(); 17 | rb?.AddForce(0, _upwardsForce, 0); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter08/Springboard.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Springboard : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Velocity change on the Y axis.")] 8 | float _upwardsForce = 300f; 9 | 10 | private void OnCollisionEnter(Collision collision) 11 | { 12 | GameObject hitObj = collision.gameObject; 13 | 14 | if (hitObj != null) 15 | { 16 | Rigidbody rb = hitObj.GetComponent(); 17 | if (rb) 18 | rb.AddForce(0, _upwardsForce, 0); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter09/Springboard.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Springboard : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Velocity change on the Y axis.")] 8 | float _upwardsForce = 300f; 9 | 10 | private void OnCollisionEnter(Collision collision) 11 | { 12 | GameObject hitObj = collision.gameObject; 13 | 14 | if (hitObj != null) 15 | { 16 | Rigidbody rb = hitObj.GetComponent(); 17 | if (rb) 18 | rb.AddForce(0, _upwardsForce, 0); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter10/Springboard.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Springboard : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Velocity change on the Y axis.")] 8 | float _upwardsForce = 300f; 9 | 10 | private void OnCollisionEnter(Collision collision) 11 | { 12 | GameObject hitObj = collision.gameObject; 13 | 14 | if (hitObj != null) 15 | { 16 | Rigidbody rb = hitObj.GetComponent(); 17 | if (rb) 18 | rb.AddForce(0, _upwardsForce, 0); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter11/Springboard.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Springboard : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Velocity change on the Y axis.")] 8 | float _upwardsForce = 300f; 9 | 10 | private void OnCollisionEnter(Collision collision) 11 | { 12 | GameObject hitObj = collision.gameObject; 13 | 14 | if (hitObj != null) 15 | { 16 | Rigidbody rb = hitObj.GetComponent(); 17 | if (rb) 18 | rb.AddForce(0, _upwardsForce, 0); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter12/Springboard.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Springboard : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Velocity change on the Y axis.")] 8 | float _upwardsForce = 300f; 9 | 10 | private void OnCollisionEnter(Collision collision) 11 | { 12 | GameObject hitObj = collision.gameObject; 13 | 14 | if (hitObj != null) 15 | { 16 | Rigidbody rb = hitObj.GetComponent(); 17 | if (rb) 18 | rb.AddForce(0, _upwardsForce, 0); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter13/Springboard.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Springboard : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Velocity change on the Y axis.")] 8 | float _upwardsForce = 300f; 9 | 10 | private void OnCollisionEnter(Collision collision) 11 | { 12 | GameObject hitObj = collision.gameObject; 13 | 14 | if (hitObj != null) 15 | { 16 | Rigidbody rb = hitObj.GetComponent(); 17 | if (rb) 18 | rb.AddForce(0, _upwardsForce, 0); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter14/Springboard.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Springboard : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Velocity change on the Y axis.")] 8 | float _upwardsForce = 300f; 9 | 10 | private void OnCollisionEnter(Collision collision) 11 | { 12 | GameObject hitObj = collision.gameObject; 13 | 14 | if (hitObj != null) 15 | { 16 | Rigidbody rb = hitObj.GetComponent(); 17 | if (rb) 18 | rb.AddForce(0, _upwardsForce, 0); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter04/TitleMenu.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class TitleMenu : MonoBehaviour 7 | { 8 | // Start is called before the first frame update 9 | void Start() 10 | { 11 | 12 | } 13 | 14 | // Update is called once per frame 15 | void Update() 16 | { 17 | 18 | } 19 | 20 | /// 21 | /// When the user presses the 'Start Game' button, 22 | /// we need to load the MainGame scene. 23 | /// 24 | public void onPressStartGameBtn() 25 | { 26 | SceneManager.LoadScene("SampleScene"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter05/TitleMenu.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class TitleMenu : MonoBehaviour 7 | { 8 | // Start is called before the first frame update 9 | void Start() 10 | { 11 | 12 | } 13 | 14 | // Update is called once per frame 15 | void Update() 16 | { 17 | 18 | } 19 | 20 | /// 21 | /// When the user presses the 'Start Game' button, 22 | /// we need to load the MainGame scene. 23 | /// 24 | public void onPressStartGameBtn() 25 | { 26 | SceneManager.LoadScene("SampleScene"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter06/TitleMenu.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class TitleMenu : MonoBehaviour 7 | { 8 | // Start is called before the first frame update 9 | void Start() 10 | { 11 | 12 | } 13 | 14 | // Update is called once per frame 15 | void Update() 16 | { 17 | 18 | } 19 | 20 | /// 21 | /// When the user presses the 'Start Game' button, 22 | /// we need to load the MainGame scene. 23 | /// 24 | public void onPressStartGameBtn() 25 | { 26 | SceneManager.LoadScene("SampleScene"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter07/TitleMenu.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class TitleMenu : MonoBehaviour 7 | { 8 | // Start is called before the first frame update 9 | void Start() 10 | { 11 | 12 | } 13 | 14 | // Update is called once per frame 15 | void Update() 16 | { 17 | 18 | } 19 | 20 | /// 21 | /// When the user presses the 'Start Game' button, 22 | /// we need to load the MainGame scene. 23 | /// 24 | public void onPressStartGameBtn() 25 | { 26 | SceneManager.LoadScene("SampleScene"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter08/TitleMenu.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class TitleMenu : MonoBehaviour 7 | { 8 | // Start is called before the first frame update 9 | void Start() 10 | { 11 | 12 | } 13 | 14 | // Update is called once per frame 15 | void Update() 16 | { 17 | 18 | } 19 | 20 | /// 21 | /// When the user presses the 'Start Game' button, 22 | /// we need to load the MainGame scene. 23 | /// 24 | public void onPressStartGameBtn() 25 | { 26 | SceneManager.LoadScene("SampleScene"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter09/TitleMenu.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class TitleMenu : MonoBehaviour 7 | { 8 | // Start is called before the first frame update 9 | void Start() 10 | { 11 | 12 | } 13 | 14 | // Update is called once per frame 15 | void Update() 16 | { 17 | 18 | } 19 | 20 | /// 21 | /// When the user presses the 'Start Game' button, 22 | /// we need to load the MainGame scene. 23 | /// 24 | public void onPressStartGameBtn() 25 | { 26 | SceneManager.LoadScene("SampleScene"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter10/TitleMenu.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class TitleMenu : MonoBehaviour 7 | { 8 | // Start is called before the first frame update 9 | void Start() 10 | { 11 | 12 | } 13 | 14 | // Update is called once per frame 15 | void Update() 16 | { 17 | 18 | } 19 | 20 | /// 21 | /// When the user presses the 'Start Game' button, 22 | /// we need to load the MainGame scene. 23 | /// 24 | public void onPressStartGameBtn() 25 | { 26 | SceneManager.LoadScene("SampleScene"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter11/TitleMenu.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class TitleMenu : MonoBehaviour 7 | { 8 | // Start is called before the first frame update 9 | void Start() 10 | { 11 | 12 | } 13 | 14 | // Update is called once per frame 15 | void Update() 16 | { 17 | 18 | } 19 | 20 | /// 21 | /// When the user presses the 'Start Game' button, 22 | /// we need to load the MainGame scene. 23 | /// 24 | public void onPressStartGameBtn() 25 | { 26 | SceneManager.LoadScene("SampleScene"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter12/TitleMenu.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class TitleMenu : MonoBehaviour 7 | { 8 | // Start is called before the first frame update 9 | void Start() 10 | { 11 | 12 | } 13 | 14 | // Update is called once per frame 15 | void Update() 16 | { 17 | 18 | } 19 | 20 | /// 21 | /// When the user presses the 'Start Game' button, 22 | /// we need to load the MainGame scene. 23 | /// 24 | public void onPressStartGameBtn() 25 | { 26 | SceneManager.LoadScene("SampleScene"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter13/TitleMenu.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class TitleMenu : MonoBehaviour 7 | { 8 | // Start is called before the first frame update 9 | void Start() 10 | { 11 | 12 | } 13 | 14 | // Update is called once per frame 15 | void Update() 16 | { 17 | 18 | } 19 | 20 | /// 21 | /// When the user presses the 'Start Game' button, 22 | /// we need to load the MainGame scene. 23 | /// 24 | public void onPressStartGameBtn() 25 | { 26 | SceneManager.LoadScene("SampleScene"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter14/TitleMenu.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class TitleMenu : MonoBehaviour 7 | { 8 | // Start is called before the first frame update 9 | void Start() 10 | { 11 | 12 | } 13 | 14 | // Update is called once per frame 15 | void Update() 16 | { 17 | 18 | } 19 | 20 | /// 21 | /// When the user presses the 'Start Game' button, 22 | /// we need to load the MainGame scene. 23 | /// 24 | public void onPressStartGameBtn() 25 | { 26 | SceneManager.LoadScene("SampleScene"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter03/Bullet.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Bullet : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Speed of this bullet.")] 9 | private float _speed = 4f; 10 | 11 | [SerializeField, Tooltip("Normalized direciton of this bullet.")] 12 | private Vector3 _direction = Vector3.zero; 13 | 14 | void Update() 15 | { 16 | // move the bullet 17 | Vector3 newPos = transform.position; 18 | newPos += _direction * (_speed * Time.deltaTime); 19 | transform.position = newPos; 20 | } 21 | 22 | public void SetDirection( Vector3 direction ) 23 | { 24 | _direction = direction; 25 | 26 | // rotate to face the direction of movement 27 | transform.LookAt(transform.position + _direction); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter04/Bullet.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Bullet : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Speed of this bullet.")] 9 | private float _speed = 4f; 10 | 11 | [SerializeField, Tooltip("Normalized direciton of this bullet.")] 12 | private Vector3 _direction = Vector3.zero; 13 | 14 | void Update() 15 | { 16 | // move the bullet 17 | Vector3 newPos = transform.position; 18 | newPos += _direction * (_speed * Time.deltaTime); 19 | transform.position = newPos; 20 | } 21 | 22 | public void SetDirection( Vector3 direction ) 23 | { 24 | _direction = direction; 25 | 26 | // rotate to face the direction of movement 27 | transform.LookAt(transform.position + _direction); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter05/Bullet.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Bullet : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Speed of this bullet.")] 9 | private float _speed = 4f; 10 | 11 | [SerializeField, Tooltip("Normalized direciton of this bullet.")] 12 | private Vector3 _direction = Vector3.zero; 13 | 14 | void Update() 15 | { 16 | // move the bullet 17 | Vector3 newPos = transform.position; 18 | newPos += _direction * (_speed * Time.deltaTime); 19 | transform.position = newPos; 20 | } 21 | 22 | public void SetDirection( Vector3 direction ) 23 | { 24 | _direction = direction; 25 | 26 | // rotate to face the direction of movement 27 | transform.LookAt(transform.position + _direction); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter06/Bullet.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Bullet : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Speed of this bullet.")] 9 | private float _speed = 4f; 10 | 11 | [SerializeField, Tooltip("Normalized direciton of this bullet.")] 12 | private Vector3 _direction = Vector3.zero; 13 | 14 | void Update() 15 | { 16 | // move the bullet 17 | Vector3 newPos = transform.position; 18 | newPos += _direction * (_speed * Time.deltaTime); 19 | transform.position = newPos; 20 | } 21 | 22 | public void SetDirection( Vector3 direction ) 23 | { 24 | _direction = direction; 25 | 26 | // rotate to face the direction of movement 27 | transform.LookAt(transform.position + _direction); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter07/Bullet.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Bullet : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Speed of this bullet.")] 9 | private float _speed = 4f; 10 | 11 | [SerializeField, Tooltip("Normalized direciton of this bullet.")] 12 | private Vector3 _direction = Vector3.zero; 13 | 14 | void Update() 15 | { 16 | // move the bullet 17 | Vector3 newPos = transform.position; 18 | newPos += _direction * (_speed * Time.deltaTime); 19 | transform.position = newPos; 20 | } 21 | 22 | public void SetDirection( Vector3 direction ) 23 | { 24 | _direction = direction; 25 | 26 | // rotate to face the direction of movement 27 | transform.LookAt(transform.position + _direction); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter08/Bullet.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Bullet : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Speed of this bullet.")] 9 | private float _speed = 4f; 10 | 11 | [SerializeField, Tooltip("Normalized direciton of this bullet.")] 12 | private Vector3 _direction = Vector3.zero; 13 | 14 | void Update() 15 | { 16 | // move the bullet 17 | Vector3 newPos = transform.position; 18 | newPos += _direction * (_speed * Time.deltaTime); 19 | transform.position = newPos; 20 | } 21 | 22 | public void SetDirection( Vector3 direction ) 23 | { 24 | _direction = direction; 25 | 26 | // rotate to face the direction of movement 27 | transform.LookAt(transform.position + _direction); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter09/Bullet.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Bullet : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Speed of this bullet.")] 9 | private float _speed = 4f; 10 | 11 | [SerializeField, Tooltip("Normalized direciton of this bullet.")] 12 | private Vector3 _direction = Vector3.zero; 13 | 14 | void Update() 15 | { 16 | // move the bullet 17 | Vector3 newPos = transform.position; 18 | newPos += _direction * (_speed * Time.deltaTime); 19 | transform.position = newPos; 20 | } 21 | 22 | public void SetDirection( Vector3 direction ) 23 | { 24 | _direction = direction; 25 | 26 | // rotate to face the direction of movement 27 | transform.LookAt(transform.position + _direction); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter10/Bullet.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Bullet : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Speed of this bullet.")] 9 | private float _speed = 4f; 10 | 11 | [SerializeField, Tooltip("Normalized direciton of this bullet.")] 12 | private Vector3 _direction = Vector3.zero; 13 | 14 | void Update() 15 | { 16 | // move the bullet 17 | Vector3 newPos = transform.position; 18 | newPos += _direction * (_speed * Time.deltaTime); 19 | transform.position = newPos; 20 | } 21 | 22 | public void SetDirection( Vector3 direction ) 23 | { 24 | _direction = direction; 25 | 26 | // rotate to face the direction of movement 27 | transform.LookAt(transform.position + _direction); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter11/Bullet.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Bullet : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Speed of this bullet.")] 9 | private float _speed = 4f; 10 | 11 | [SerializeField, Tooltip("Normalized direciton of this bullet.")] 12 | private Vector3 _direction = Vector3.zero; 13 | 14 | void Update() 15 | { 16 | // move the bullet 17 | Vector3 newPos = transform.position; 18 | newPos += _direction * (_speed * Time.deltaTime); 19 | transform.position = newPos; 20 | } 21 | 22 | public void SetDirection( Vector3 direction ) 23 | { 24 | _direction = direction; 25 | 26 | // rotate to face the direction of movement 27 | transform.LookAt(transform.position + _direction); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter12/Bullet.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Bullet : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Speed of this bullet.")] 9 | private float _speed = 4f; 10 | 11 | [SerializeField, Tooltip("Normalized direciton of this bullet.")] 12 | private Vector3 _direction = Vector3.zero; 13 | 14 | void Update() 15 | { 16 | // move the bullet 17 | Vector3 newPos = transform.position; 18 | newPos += _direction * (_speed * Time.deltaTime); 19 | transform.position = newPos; 20 | } 21 | 22 | public void SetDirection( Vector3 direction ) 23 | { 24 | _direction = direction; 25 | 26 | // rotate to face the direction of movement 27 | transform.LookAt(transform.position + _direction); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter13/Bullet.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Bullet : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Speed of this bullet.")] 9 | private float _speed = 4f; 10 | 11 | [SerializeField, Tooltip("Normalized direciton of this bullet.")] 12 | private Vector3 _direction = Vector3.zero; 13 | 14 | void Update() 15 | { 16 | // move the bullet 17 | Vector3 newPos = transform.position; 18 | newPos += _direction * (_speed * Time.deltaTime); 19 | transform.position = newPos; 20 | } 21 | 22 | public void SetDirection( Vector3 direction ) 23 | { 24 | _direction = direction; 25 | 26 | // rotate to face the direction of movement 27 | transform.LookAt(transform.position + _direction); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter14/Bullet.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Bullet : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Speed of this bullet.")] 9 | private float _speed = 4f; 10 | 11 | [SerializeField, Tooltip("Normalized direciton of this bullet.")] 12 | private Vector3 _direction = Vector3.zero; 13 | 14 | void Update() 15 | { 16 | // move the bullet 17 | Vector3 newPos = transform.position; 18 | newPos += _direction * (_speed * Time.deltaTime); 19 | transform.position = newPos; 20 | } 21 | 22 | public void SetDirection( Vector3 direction ) 23 | { 24 | _direction = direction; 25 | 26 | // rotate to face the direction of movement 27 | transform.LookAt(transform.position + _direction); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter03/CameraShake.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraShake : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Magnitude of the shake effect.")] 9 | float _shake = 0.05f; 10 | 11 | Vector3 _startPos; 12 | 13 | private void Start() 14 | { 15 | // store the starting position 16 | _startPos = transform.position; 17 | } 18 | 19 | void Update() 20 | { 21 | // if enabled, give camera a little shake 22 | Vector3 newPosition = new Vector3(); 23 | newPosition.x = _startPos.x + Random.Range(-_shake, _shake); 24 | newPosition.y = _startPos.y + Random.Range(-_shake, _shake); 25 | newPosition.z = _startPos.z + Random.Range(-_shake, _shake); 26 | 27 | transform.position = newPosition; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter04/CameraShake.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraShake : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Magnitude of the shake effect.")] 9 | float _shake = 0.05f; 10 | 11 | Vector3 _startPos; 12 | 13 | private void Start() 14 | { 15 | // store the starting position 16 | _startPos = transform.position; 17 | } 18 | 19 | void Update() 20 | { 21 | // if enabled, give camera a little shake 22 | Vector3 newPosition = new Vector3(); 23 | newPosition.x = _startPos.x + Random.Range(-_shake, _shake); 24 | newPosition.y = _startPos.y + Random.Range(-_shake, _shake); 25 | newPosition.z = _startPos.z + Random.Range(-_shake, _shake); 26 | 27 | transform.position = newPosition; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter05/CameraShake.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraShake : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Magnitude of the shake effect.")] 9 | float _shake = 0.05f; 10 | 11 | Vector3 _startPos; 12 | 13 | private void Start() 14 | { 15 | // store the starting position 16 | _startPos = transform.position; 17 | } 18 | 19 | void Update() 20 | { 21 | // if enabled, give camera a little shake 22 | Vector3 newPosition = new Vector3(); 23 | newPosition.x = _startPos.x + Random.Range(-_shake, _shake); 24 | newPosition.y = _startPos.y + Random.Range(-_shake, _shake); 25 | newPosition.z = _startPos.z + Random.Range(-_shake, _shake); 26 | 27 | transform.position = newPosition; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter06/CameraShake.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraShake : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Magnitude of the shake effect.")] 9 | float _shake = 0.05f; 10 | 11 | Vector3 _startPos; 12 | 13 | private void Start() 14 | { 15 | // store the starting position 16 | _startPos = transform.position; 17 | } 18 | 19 | void Update() 20 | { 21 | // if enabled, give camera a little shake 22 | Vector3 newPosition = new Vector3(); 23 | newPosition.x = _startPos.x + Random.Range(-_shake, _shake); 24 | newPosition.y = _startPos.y + Random.Range(-_shake, _shake); 25 | newPosition.z = _startPos.z + Random.Range(-_shake, _shake); 26 | 27 | transform.position = newPosition; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter07/CameraShake.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraShake : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Magnitude of the shake effect.")] 9 | float _shake = 0.05f; 10 | 11 | Vector3 _startPos; 12 | 13 | private void Start() 14 | { 15 | // store the starting position 16 | _startPos = transform.position; 17 | } 18 | 19 | void Update() 20 | { 21 | // if enabled, give camera a little shake 22 | Vector3 newPosition = new Vector3(); 23 | newPosition.x = _startPos.x + Random.Range(-_shake, _shake); 24 | newPosition.y = _startPos.y + Random.Range(-_shake, _shake); 25 | newPosition.z = _startPos.z + Random.Range(-_shake, _shake); 26 | 27 | transform.position = newPosition; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter08/CameraShake.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraShake : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Magnitude of the shake effect.")] 9 | float _shake = 0.05f; 10 | 11 | Vector3 _startPos; 12 | 13 | private void Start() 14 | { 15 | // store the starting position 16 | _startPos = transform.position; 17 | } 18 | 19 | void Update() 20 | { 21 | // if enabled, give camera a little shake 22 | Vector3 newPosition = new Vector3(); 23 | newPosition.x = _startPos.x + Random.Range(-_shake, _shake); 24 | newPosition.y = _startPos.y + Random.Range(-_shake, _shake); 25 | newPosition.z = _startPos.z + Random.Range(-_shake, _shake); 26 | 27 | transform.position = newPosition; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter09/CameraShake.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraShake : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Magnitude of the shake effect.")] 9 | float _shake = 0.05f; 10 | 11 | Vector3 _startPos; 12 | 13 | private void Start() 14 | { 15 | // store the starting position 16 | _startPos = transform.position; 17 | } 18 | 19 | void Update() 20 | { 21 | // if enabled, give camera a little shake 22 | Vector3 newPosition = new Vector3(); 23 | newPosition.x = _startPos.x + Random.Range(-_shake, _shake); 24 | newPosition.y = _startPos.y + Random.Range(-_shake, _shake); 25 | newPosition.z = _startPos.z + Random.Range(-_shake, _shake); 26 | 27 | transform.position = newPosition; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter10/CameraShake.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraShake : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Magnitude of the shake effect.")] 9 | float _shake = 0.05f; 10 | 11 | Vector3 _startPos; 12 | 13 | private void Start() 14 | { 15 | // store the starting position 16 | _startPos = transform.position; 17 | } 18 | 19 | void Update() 20 | { 21 | // if enabled, give camera a little shake 22 | Vector3 newPosition = new Vector3(); 23 | newPosition.x = _startPos.x + Random.Range(-_shake, _shake); 24 | newPosition.y = _startPos.y + Random.Range(-_shake, _shake); 25 | newPosition.z = _startPos.z + Random.Range(-_shake, _shake); 26 | 27 | transform.position = newPosition; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter11/CameraShake.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraShake : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Magnitude of the shake effect.")] 9 | float _shake = 0.05f; 10 | 11 | Vector3 _startPos; 12 | 13 | private void Start() 14 | { 15 | // store the starting position 16 | _startPos = transform.position; 17 | } 18 | 19 | void Update() 20 | { 21 | // if enabled, give camera a little shake 22 | Vector3 newPosition = new Vector3(); 23 | newPosition.x = _startPos.x + Random.Range(-_shake, _shake); 24 | newPosition.y = _startPos.y + Random.Range(-_shake, _shake); 25 | newPosition.z = _startPos.z + Random.Range(-_shake, _shake); 26 | 27 | transform.position = newPosition; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter12/CameraShake.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraShake : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Magnitude of the shake effect.")] 9 | float _shake = 0.05f; 10 | 11 | Vector3 _startPos; 12 | 13 | private void Start() 14 | { 15 | // store the starting position 16 | _startPos = transform.position; 17 | } 18 | 19 | void Update() 20 | { 21 | // if enabled, give camera a little shake 22 | Vector3 newPosition = new Vector3(); 23 | newPosition.x = _startPos.x + Random.Range(-_shake, _shake); 24 | newPosition.y = _startPos.y + Random.Range(-_shake, _shake); 25 | newPosition.z = _startPos.z + Random.Range(-_shake, _shake); 26 | 27 | transform.position = newPosition; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter13/CameraShake.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraShake : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Magnitude of the shake effect.")] 9 | float _shake = 0.05f; 10 | 11 | Vector3 _startPos; 12 | 13 | private void Start() 14 | { 15 | // store the starting position 16 | _startPos = transform.position; 17 | } 18 | 19 | void Update() 20 | { 21 | // if enabled, give camera a little shake 22 | Vector3 newPosition = new Vector3(); 23 | newPosition.x = _startPos.x + Random.Range(-_shake, _shake); 24 | newPosition.y = _startPos.y + Random.Range(-_shake, _shake); 25 | newPosition.z = _startPos.z + Random.Range(-_shake, _shake); 26 | 27 | transform.position = newPosition; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter14/CameraShake.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraShake : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Magnitude of the shake effect.")] 9 | float _shake = 0.05f; 10 | 11 | Vector3 _startPos; 12 | 13 | private void Start() 14 | { 15 | // store the starting position 16 | _startPos = transform.position; 17 | } 18 | 19 | void Update() 20 | { 21 | // if enabled, give camera a little shake 22 | Vector3 newPosition = new Vector3(); 23 | newPosition.x = _startPos.x + Random.Range(-_shake, _shake); 24 | newPosition.y = _startPos.y + Random.Range(-_shake, _shake); 25 | newPosition.z = _startPos.z + Random.Range(-_shake, _shake); 26 | 27 | transform.position = newPosition; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter10/SpawnedSoundFX.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SpawnedSoundFX : MonoBehaviour 6 | { 7 | public AudioSource _audioSource; 8 | 9 | public static void Spawn( Vector3 pos, AudioClip clip = null ) 10 | { 11 | // spawn soundFX object 12 | GameObject prefab = Resources.Load("Prefabs/SpawnedSoundFX"); 13 | GameObject newObj = Instantiate( prefab, pos, Quaternion.identity); 14 | 15 | // add randomness to pitch 16 | float rand = Random.Range(0.95f, 1.05f); 17 | SpawnedSoundFX soundScript = newObj.GetComponent(); 18 | soundScript._audioSource.pitch = rand; 19 | 20 | // swap audio clip 21 | if (clip) 22 | { 23 | soundScript._audioSource.clip = clip; 24 | soundScript._audioSource.Play(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter11/SpawnedSoundFX.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SpawnedSoundFX : MonoBehaviour 6 | { 7 | public AudioSource _audioSource; 8 | 9 | public static void Spawn( Vector3 pos, AudioClip clip = null ) 10 | { 11 | // spawn soundFX object 12 | GameObject prefab = Resources.Load("Prefabs/SpawnedSoundFX"); 13 | GameObject newObj = Instantiate( prefab, pos, Quaternion.identity); 14 | 15 | // add randomness to pitch 16 | float rand = Random.Range(0.95f, 1.05f); 17 | SpawnedSoundFX soundScript = newObj.GetComponent(); 18 | soundScript._audioSource.pitch = rand; 19 | 20 | // swap audio clip 21 | if (clip) 22 | { 23 | soundScript._audioSource.clip = clip; 24 | soundScript._audioSource.Play(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter12/SpawnedSoundFX.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SpawnedSoundFX : MonoBehaviour 6 | { 7 | public AudioSource _audioSource; 8 | 9 | public static void Spawn( Vector3 pos, AudioClip clip = null ) 10 | { 11 | // spawn soundFX object 12 | GameObject prefab = Resources.Load("Prefabs/SpawnedSoundFX"); 13 | GameObject newObj = Instantiate( prefab, pos, Quaternion.identity); 14 | 15 | // add randomness to pitch 16 | float rand = Random.Range(0.95f, 1.05f); 17 | SpawnedSoundFX soundScript = newObj.GetComponent(); 18 | soundScript._audioSource.pitch = rand; 19 | 20 | // swap audio clip 21 | if (clip) 22 | { 23 | soundScript._audioSource.clip = clip; 24 | soundScript._audioSource.Play(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter13/SpawnedSoundFX.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SpawnedSoundFX : MonoBehaviour 6 | { 7 | public AudioSource _audioSource; 8 | 9 | public static void Spawn( Vector3 pos, AudioClip clip = null ) 10 | { 11 | // spawn soundFX object 12 | GameObject prefab = Resources.Load("Prefabs/SpawnedSoundFX"); 13 | GameObject newObj = Instantiate( prefab, pos, Quaternion.identity); 14 | 15 | // add randomness to pitch 16 | float rand = Random.Range(0.95f, 1.05f); 17 | SpawnedSoundFX soundScript = newObj.GetComponent(); 18 | soundScript._audioSource.pitch = rand; 19 | 20 | // swap audio clip 21 | if (clip) 22 | { 23 | soundScript._audioSource.clip = clip; 24 | soundScript._audioSource.Play(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter14/SpawnedSoundFX.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SpawnedSoundFX : MonoBehaviour 6 | { 7 | public AudioSource _audioSource; 8 | 9 | public static void Spawn( Vector3 pos, AudioClip clip = null ) 10 | { 11 | // spawn soundFX object 12 | GameObject prefab = Resources.Load("Prefabs/SpawnedSoundFX"); 13 | GameObject newObj = Instantiate( prefab, pos, Quaternion.identity); 14 | 15 | // add randomness to pitch 16 | float rand = Random.Range(0.95f, 1.05f); 17 | SpawnedSoundFX soundScript = newObj.GetComponent(); 18 | soundScript._audioSource.pitch = rand; 19 | 20 | // swap audio clip 21 | if (clip) 22 | { 23 | soundScript._audioSource.clip = clip; 24 | soundScript._audioSource.Play(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter02/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | } 16 | 17 | // Update is called once per frame 18 | void Update() 19 | { 20 | // apply rotation 21 | Vector3 newRotation = transform.eulerAngles; 22 | newRotation.y += (_rotationSpeed * Time.deltaTime); 23 | transform.eulerAngles = newRotation; 24 | } 25 | 26 | public void onPickedUp( GameObject whoPickedUp ) 27 | { 28 | // show the collection count in the console window 29 | s_objectsCollected++; 30 | Debug.Log( s_objectsCollected + " items picked up." ); 31 | 32 | // destroy the item 33 | Destroy(gameObject); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Chapter03/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | } 16 | 17 | // Update is called once per frame 18 | void Update() 19 | { 20 | // apply rotation 21 | Vector3 newRotation = transform.eulerAngles; 22 | newRotation.y += (_rotationSpeed * Time.deltaTime); 23 | transform.eulerAngles = newRotation; 24 | } 25 | 26 | public void onPickedUp( GameObject whoPickedUp ) 27 | { 28 | // show the collection count in the console window 29 | s_objectsCollected++; 30 | Debug.Log( s_objectsCollected + " items picked up." ); 31 | 32 | // destroy the item 33 | Destroy(gameObject); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Chapter04/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | } 16 | 17 | // Update is called once per frame 18 | void Update() 19 | { 20 | // apply rotation 21 | Vector3 newRotation = transform.eulerAngles; 22 | newRotation.y += (_rotationSpeed * Time.deltaTime); 23 | transform.eulerAngles = newRotation; 24 | } 25 | 26 | public void onPickedUp( GameObject whoPickedUp ) 27 | { 28 | // show the collection count in the console window 29 | s_objectsCollected++; 30 | Debug.Log( s_objectsCollected + " items picked up." ); 31 | 32 | // destroy the item 33 | Destroy(gameObject); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Chapter05/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | 16 | } 17 | 18 | // Update is called once per frame 19 | void Update() 20 | { 21 | // apply rotation 22 | Vector3 newRotation = transform.eulerAngles; 23 | newRotation.y += (_rotationSpeed * Time.deltaTime); 24 | transform.eulerAngles = newRotation; 25 | } 26 | 27 | public void onPickedUp( GameObject whoPickedUp ) 28 | { 29 | // show the collection count in the console window 30 | s_objectsCollected++; 31 | Debug.Log( s_objectsCollected + " items picked up." ); 32 | 33 | // destroy the item 34 | Destroy(gameObject); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Chapter06/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | 16 | } 17 | 18 | // Update is called once per frame 19 | void Update() 20 | { 21 | // apply rotation 22 | Vector3 newRotation = transform.eulerAngles; 23 | newRotation.y += (_rotationSpeed * Time.deltaTime); 24 | transform.eulerAngles = newRotation; 25 | } 26 | 27 | public void onPickedUp( GameObject whoPickedUp ) 28 | { 29 | // show the collection count in the console window 30 | s_objectsCollected++; 31 | Debug.Log( s_objectsCollected + " items picked up." ); 32 | 33 | // destroy the item 34 | Destroy(gameObject); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Chapter07/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | 16 | } 17 | 18 | // Update is called once per frame 19 | void Update() 20 | { 21 | // apply rotation 22 | Vector3 newRotation = transform.eulerAngles; 23 | newRotation.y += (_rotationSpeed * Time.deltaTime); 24 | transform.eulerAngles = newRotation; 25 | } 26 | 27 | public void onPickedUp( GameObject whoPickedUp ) 28 | { 29 | // show the collection count in the console window 30 | s_objectsCollected++; 31 | Debug.Log( s_objectsCollected + " items picked up." ); 32 | 33 | // destroy the item 34 | Destroy(gameObject); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Chapter08/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | 16 | } 17 | 18 | // Update is called once per frame 19 | void Update() 20 | { 21 | // apply rotation 22 | Vector3 newRotation = transform.eulerAngles; 23 | newRotation.y += (_rotationSpeed * Time.deltaTime); 24 | transform.eulerAngles = newRotation; 25 | } 26 | 27 | public void onPickedUp( GameObject whoPickedUp ) 28 | { 29 | // show the collection count in the console window 30 | s_objectsCollected++; 31 | Debug.Log( s_objectsCollected + " items picked up." ); 32 | 33 | // destroy the item 34 | Destroy(gameObject); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 BPB Online 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Chapter06/PhysicsForceZone.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PhysicsForceZone : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Force applied to any hit RigidBody object.")] 8 | float _forceToApply; 9 | 10 | private void Awake() 11 | { 12 | CapsuleCollider c = GetComponent(); 13 | if (c) 14 | { 15 | c.isTrigger = true; 16 | } 17 | 18 | Rigidbody rb = GetComponent(); 19 | if (rb) 20 | { 21 | rb.isKinematic = true; 22 | rb.useGravity = false; 23 | } 24 | } 25 | 26 | void OnTriggerStay(Collider collider) 27 | { 28 | GameObject hitObj = collider.gameObject; 29 | 30 | if ( hitObj != null ) 31 | { 32 | Rigidbody rb = hitObj.GetComponent(); 33 | 34 | // get the direction of the Y axis 35 | Vector3 dir = transform.up; 36 | 37 | // apply directional force to this object 38 | rb.AddForce( dir * _forceToApply ); 39 | 40 | } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter07/PhysicsForceZone.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PhysicsForceZone : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Force applied to any hit RigidBody object.")] 8 | float _forceToApply; 9 | 10 | private void Awake() 11 | { 12 | CapsuleCollider c = GetComponent(); 13 | if (c) 14 | { 15 | c.isTrigger = true; 16 | } 17 | 18 | Rigidbody rb = GetComponent(); 19 | if (rb) 20 | { 21 | rb.isKinematic = true; 22 | rb.useGravity = false; 23 | } 24 | } 25 | 26 | void OnTriggerStay(Collider collider) 27 | { 28 | GameObject hitObj = collider.gameObject; 29 | 30 | if ( hitObj != null ) 31 | { 32 | Rigidbody rb = hitObj.GetComponent(); 33 | 34 | // get the direction of the Y axis 35 | Vector3 dir = transform.up; 36 | 37 | // apply directional force to this object 38 | rb.AddForce( dir * _forceToApply ); 39 | 40 | } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter08/PhysicsForceZone.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PhysicsForceZone : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Force applied to any hit RigidBody object.")] 8 | float _forceToApply; 9 | 10 | private void Awake() 11 | { 12 | CapsuleCollider c = GetComponent(); 13 | if (c) 14 | { 15 | c.isTrigger = true; 16 | } 17 | 18 | Rigidbody rb = GetComponent(); 19 | if (rb) 20 | { 21 | rb.isKinematic = true; 22 | rb.useGravity = false; 23 | } 24 | } 25 | 26 | void OnTriggerStay(Collider collider) 27 | { 28 | GameObject hitObj = collider.gameObject; 29 | 30 | if ( hitObj != null ) 31 | { 32 | Rigidbody rb = hitObj.GetComponent(); 33 | 34 | // get the direction of the Y axis 35 | Vector3 dir = transform.up; 36 | 37 | // apply directional force to this object 38 | rb.AddForce( dir * _forceToApply ); 39 | 40 | } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter09/PhysicsForceZone.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PhysicsForceZone : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Force applied to any hit RigidBody object.")] 8 | float _forceToApply; 9 | 10 | private void Awake() 11 | { 12 | CapsuleCollider c = GetComponent(); 13 | if (c) 14 | { 15 | c.isTrigger = true; 16 | } 17 | 18 | Rigidbody rb = GetComponent(); 19 | if (rb) 20 | { 21 | rb.isKinematic = true; 22 | rb.useGravity = false; 23 | } 24 | } 25 | 26 | void OnTriggerStay(Collider collider) 27 | { 28 | GameObject hitObj = collider.gameObject; 29 | 30 | if ( hitObj != null ) 31 | { 32 | Rigidbody rb = hitObj.GetComponent(); 33 | 34 | // get the direction of the Y axis 35 | Vector3 dir = transform.up; 36 | 37 | // apply directional force to this object 38 | rb.AddForce( dir * _forceToApply ); 39 | 40 | } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter10/PhysicsForceZone.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PhysicsForceZone : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Force applied to any hit RigidBody object.")] 8 | float _forceToApply; 9 | 10 | private void Awake() 11 | { 12 | CapsuleCollider c = GetComponent(); 13 | if (c) 14 | { 15 | c.isTrigger = true; 16 | } 17 | 18 | Rigidbody rb = GetComponent(); 19 | if (rb) 20 | { 21 | rb.isKinematic = true; 22 | rb.useGravity = false; 23 | } 24 | } 25 | 26 | void OnTriggerStay(Collider collider) 27 | { 28 | GameObject hitObj = collider.gameObject; 29 | 30 | if ( hitObj != null ) 31 | { 32 | Rigidbody rb = hitObj.GetComponent(); 33 | 34 | // get the direction of the Y axis 35 | Vector3 dir = transform.up; 36 | 37 | // apply directional force to this object 38 | rb.AddForce( dir * _forceToApply ); 39 | 40 | } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter11/PhysicsForceZone.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PhysicsForceZone : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Force applied to any hit RigidBody object.")] 8 | float _forceToApply; 9 | 10 | private void Awake() 11 | { 12 | CapsuleCollider c = GetComponent(); 13 | if (c) 14 | { 15 | c.isTrigger = true; 16 | } 17 | 18 | Rigidbody rb = GetComponent(); 19 | if (rb) 20 | { 21 | rb.isKinematic = true; 22 | rb.useGravity = false; 23 | } 24 | } 25 | 26 | void OnTriggerStay(Collider collider) 27 | { 28 | GameObject hitObj = collider.gameObject; 29 | 30 | if ( hitObj != null ) 31 | { 32 | Rigidbody rb = hitObj.GetComponent(); 33 | 34 | // get the direction of the Y axis 35 | Vector3 dir = transform.up; 36 | 37 | // apply directional force to this object 38 | rb.AddForce( dir * _forceToApply ); 39 | 40 | } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter12/PhysicsForceZone.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PhysicsForceZone : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Force applied to any hit RigidBody object.")] 8 | float _forceToApply; 9 | 10 | private void Awake() 11 | { 12 | CapsuleCollider c = GetComponent(); 13 | if (c) 14 | { 15 | c.isTrigger = true; 16 | } 17 | 18 | Rigidbody rb = GetComponent(); 19 | if (rb) 20 | { 21 | rb.isKinematic = true; 22 | rb.useGravity = false; 23 | } 24 | } 25 | 26 | void OnTriggerStay(Collider collider) 27 | { 28 | GameObject hitObj = collider.gameObject; 29 | 30 | if ( hitObj != null ) 31 | { 32 | Rigidbody rb = hitObj.GetComponent(); 33 | 34 | // get the direction of the Y axis 35 | Vector3 dir = transform.up; 36 | 37 | // apply directional force to this object 38 | rb.AddForce( dir * _forceToApply ); 39 | 40 | } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter13/PhysicsForceZone.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PhysicsForceZone : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Force applied to any hit RigidBody object.")] 8 | float _forceToApply; 9 | 10 | private void Awake() 11 | { 12 | CapsuleCollider c = GetComponent(); 13 | if (c) 14 | { 15 | c.isTrigger = true; 16 | } 17 | 18 | Rigidbody rb = GetComponent(); 19 | if (rb) 20 | { 21 | rb.isKinematic = true; 22 | rb.useGravity = false; 23 | } 24 | } 25 | 26 | void OnTriggerStay(Collider collider) 27 | { 28 | GameObject hitObj = collider.gameObject; 29 | 30 | if ( hitObj != null ) 31 | { 32 | Rigidbody rb = hitObj.GetComponent(); 33 | 34 | // get the direction of the Y axis 35 | Vector3 dir = transform.up; 36 | 37 | // apply directional force to this object 38 | rb.AddForce( dir * _forceToApply ); 39 | 40 | } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter14/PhysicsForceZone.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PhysicsForceZone : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Force applied to any hit RigidBody object.")] 8 | float _forceToApply; 9 | 10 | private void Awake() 11 | { 12 | CapsuleCollider c = GetComponent(); 13 | if (c) 14 | { 15 | c.isTrigger = true; 16 | } 17 | 18 | Rigidbody rb = GetComponent(); 19 | if (rb) 20 | { 21 | rb.isKinematic = true; 22 | rb.useGravity = false; 23 | } 24 | } 25 | 26 | void OnTriggerStay(Collider collider) 27 | { 28 | GameObject hitObj = collider.gameObject; 29 | 30 | if ( hitObj != null ) 31 | { 32 | Rigidbody rb = hitObj.GetComponent(); 33 | 34 | // get the direction of the Y axis 35 | Vector3 dir = transform.up; 36 | 37 | // apply directional force to this object 38 | rb.AddForce( dir * _forceToApply ); 39 | 40 | } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter06/ToggleTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class ToggleTimer : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Current timer.")] 9 | float _curTimer = 0; 10 | 11 | [SerializeField, Tooltip("Seconds between toggling of objects.")] 12 | float _timerGoal = 3; 13 | 14 | [SerializeField, Tooltip("Objects to toggle on/off.")] 15 | List _toggleObjs; 16 | 17 | void Update() 18 | { 19 | // if there are no objects to toggle 20 | // then don't bother with the countdown logic 21 | if (_toggleObjs == null) 22 | return; 23 | 24 | // increment timer 25 | _curTimer += Time.deltaTime; 26 | 27 | // have we met our goal? 28 | if ( _curTimer > _timerGoal ) 29 | { 30 | // reset timer 31 | _curTimer = 0; 32 | 33 | // go through objects and toggle on/off 34 | for ( int i = 0; i < _toggleObjs.Count; i++ ) 35 | { 36 | bool newVal = !_toggleObjs[i].activeSelf; 37 | _toggleObjs[i].SetActive(newVal); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter07/ToggleTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class ToggleTimer : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Current timer.")] 9 | float _curTimer = 0; 10 | 11 | [SerializeField, Tooltip("Seconds between toggling of objects.")] 12 | float _timerGoal = 3; 13 | 14 | [SerializeField, Tooltip("Objects to toggle on/off.")] 15 | List _toggleObjs; 16 | 17 | void Update() 18 | { 19 | // if there are no objects to toggle 20 | // then don't bother with the countdown logic 21 | if (_toggleObjs == null) 22 | return; 23 | 24 | // increment timer 25 | _curTimer += Time.deltaTime; 26 | 27 | // have we met our goal? 28 | if ( _curTimer > _timerGoal ) 29 | { 30 | // reset timer 31 | _curTimer = 0; 32 | 33 | // go through objects and toggle on/off 34 | for ( int i = 0; i < _toggleObjs.Count; i++ ) 35 | { 36 | bool newVal = !_toggleObjs[i].activeSelf; 37 | _toggleObjs[i].SetActive(newVal); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter08/ToggleTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class ToggleTimer : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Current timer.")] 9 | float _curTimer = 0; 10 | 11 | [SerializeField, Tooltip("Seconds between toggling of objects.")] 12 | float _timerGoal = 3; 13 | 14 | [SerializeField, Tooltip("Objects to toggle on/off.")] 15 | List _toggleObjs; 16 | 17 | void Update() 18 | { 19 | // if there are no objects to toggle 20 | // then don't bother with the countdown logic 21 | if (_toggleObjs == null) 22 | return; 23 | 24 | // increment timer 25 | _curTimer += Time.deltaTime; 26 | 27 | // have we met our goal? 28 | if ( _curTimer > _timerGoal ) 29 | { 30 | // reset timer 31 | _curTimer = 0; 32 | 33 | // go through objects and toggle on/off 34 | for ( int i = 0; i < _toggleObjs.Count; i++ ) 35 | { 36 | bool newVal = !_toggleObjs[i].activeSelf; 37 | _toggleObjs[i].SetActive(newVal); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter09/ToggleTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class ToggleTimer : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Current timer.")] 9 | float _curTimer = 0; 10 | 11 | [SerializeField, Tooltip("Seconds between toggling of objects.")] 12 | float _timerGoal = 3; 13 | 14 | [SerializeField, Tooltip("Objects to toggle on/off.")] 15 | List _toggleObjs; 16 | 17 | void Update() 18 | { 19 | // if there are no objects to toggle 20 | // then don't bother with the countdown logic 21 | if (_toggleObjs == null) 22 | return; 23 | 24 | // increment timer 25 | _curTimer += Time.deltaTime; 26 | 27 | // have we met our goal? 28 | if ( _curTimer > _timerGoal ) 29 | { 30 | // reset timer 31 | _curTimer = 0; 32 | 33 | // go through objects and toggle on/off 34 | for ( int i = 0; i < _toggleObjs.Count; i++ ) 35 | { 36 | bool newVal = !_toggleObjs[i].activeSelf; 37 | _toggleObjs[i].SetActive(newVal); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter10/ToggleTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class ToggleTimer : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Current timer.")] 9 | float _curTimer = 0; 10 | 11 | [SerializeField, Tooltip("Seconds between toggling of objects.")] 12 | float _timerGoal = 3; 13 | 14 | [SerializeField, Tooltip("Objects to toggle on/off.")] 15 | List _toggleObjs; 16 | 17 | void Update() 18 | { 19 | // if there are no objects to toggle 20 | // then don't bother with the countdown logic 21 | if (_toggleObjs == null) 22 | return; 23 | 24 | // increment timer 25 | _curTimer += Time.deltaTime; 26 | 27 | // have we met our goal? 28 | if ( _curTimer > _timerGoal ) 29 | { 30 | // reset timer 31 | _curTimer = 0; 32 | 33 | // go through objects and toggle on/off 34 | for ( int i = 0; i < _toggleObjs.Count; i++ ) 35 | { 36 | bool newVal = !_toggleObjs[i].activeSelf; 37 | _toggleObjs[i].SetActive(newVal); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter11/ToggleTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class ToggleTimer : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Current timer.")] 9 | float _curTimer = 0; 10 | 11 | [SerializeField, Tooltip("Seconds between toggling of objects.")] 12 | float _timerGoal = 3; 13 | 14 | [SerializeField, Tooltip("Objects to toggle on/off.")] 15 | List _toggleObjs; 16 | 17 | void Update() 18 | { 19 | // if there are no objects to toggle 20 | // then don't bother with the countdown logic 21 | if (_toggleObjs == null) 22 | return; 23 | 24 | // increment timer 25 | _curTimer += Time.deltaTime; 26 | 27 | // have we met our goal? 28 | if ( _curTimer > _timerGoal ) 29 | { 30 | // reset timer 31 | _curTimer = 0; 32 | 33 | // go through objects and toggle on/off 34 | for ( int i = 0; i < _toggleObjs.Count; i++ ) 35 | { 36 | bool newVal = !_toggleObjs[i].activeSelf; 37 | _toggleObjs[i].SetActive(newVal); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter12/ToggleTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class ToggleTimer : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Current timer.")] 9 | float _curTimer = 0; 10 | 11 | [SerializeField, Tooltip("Seconds between toggling of objects.")] 12 | float _timerGoal = 3; 13 | 14 | [SerializeField, Tooltip("Objects to toggle on/off.")] 15 | List _toggleObjs; 16 | 17 | void Update() 18 | { 19 | // if there are no objects to toggle 20 | // then don't bother with the countdown logic 21 | if (_toggleObjs == null) 22 | return; 23 | 24 | // increment timer 25 | _curTimer += Time.deltaTime; 26 | 27 | // have we met our goal? 28 | if ( _curTimer > _timerGoal ) 29 | { 30 | // reset timer 31 | _curTimer = 0; 32 | 33 | // go through objects and toggle on/off 34 | for ( int i = 0; i < _toggleObjs.Count; i++ ) 35 | { 36 | bool newVal = !_toggleObjs[i].activeSelf; 37 | _toggleObjs[i].SetActive(newVal); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter13/ToggleTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class ToggleTimer : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Current timer.")] 9 | float _curTimer = 0; 10 | 11 | [SerializeField, Tooltip("Seconds between toggling of objects.")] 12 | float _timerGoal = 3; 13 | 14 | [SerializeField, Tooltip("Objects to toggle on/off.")] 15 | List _toggleObjs; 16 | 17 | void Update() 18 | { 19 | // if there are no objects to toggle 20 | // then don't bother with the countdown logic 21 | if (_toggleObjs == null) 22 | return; 23 | 24 | // increment timer 25 | _curTimer += Time.deltaTime; 26 | 27 | // have we met our goal? 28 | if ( _curTimer > _timerGoal ) 29 | { 30 | // reset timer 31 | _curTimer = 0; 32 | 33 | // go through objects and toggle on/off 34 | for ( int i = 0; i < _toggleObjs.Count; i++ ) 35 | { 36 | bool newVal = !_toggleObjs[i].activeSelf; 37 | _toggleObjs[i].SetActive(newVal); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter14/ToggleTimer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class ToggleTimer : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Current timer.")] 9 | float _curTimer = 0; 10 | 11 | [SerializeField, Tooltip("Seconds between toggling of objects.")] 12 | float _timerGoal = 3; 13 | 14 | [SerializeField, Tooltip("Objects to toggle on/off.")] 15 | List _toggleObjs; 16 | 17 | void Update() 18 | { 19 | // if there are no objects to toggle 20 | // then don't bother with the countdown logic 21 | if (_toggleObjs == null) 22 | return; 23 | 24 | // increment timer 25 | _curTimer += Time.deltaTime; 26 | 27 | // have we met our goal? 28 | if ( _curTimer > _timerGoal ) 29 | { 30 | // reset timer 31 | _curTimer = 0; 32 | 33 | // go through objects and toggle on/off 34 | for ( int i = 0; i < _toggleObjs.Count; i++ ) 35 | { 36 | bool newVal = !_toggleObjs[i].activeSelf; 37 | _toggleObjs[i].SetActive(newVal); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter14/WorldSpacePrompt.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using TMPro; 5 | 6 | public class WorldSpacePrompt : MonoBehaviour 7 | { 8 | [TextArea, SerializeField, Tooltip ("The text to display.")] 9 | private string _promptText; 10 | 11 | [SerializeField, Tooltip("The TextMesh UI Object.")] 12 | public TextMeshProUGUI _textObj; 13 | 14 | [SerializeField, Tooltip("The Prompt Parent to toggle.")] 15 | public GameObject _promptBG; 16 | 17 | // Start is called before the first frame update 18 | void Start() 19 | { 20 | _textObj.SetText( _promptText ); 21 | } 22 | 23 | private void OnCollisionEnter(Collision collision) 24 | { 25 | // if the player collides with this object, 26 | // unhide up the prompt text 27 | if ( collision.gameObject.GetComponent() ) 28 | _promptBG.SetActive(true); 29 | } 30 | 31 | private void OnCollisionExit(Collision collision) 32 | { 33 | // if the player contact ends, 34 | // then hide up the prompt text 35 | if (collision.gameObject.GetComponent()) 36 | _promptBG.SetActive(false); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Chapter05/FollowCam.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class FollowCam : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The object to follow.")] 8 | private GameObject _camTarget; 9 | 10 | [SerializeField, Tooltip("Target offset.")] 11 | private Vector3 _targetOffset; 12 | 13 | [SerializeField, Tooltip("The height off the ground to follow from.")] 14 | private float _camHeight = 10; 15 | 16 | [SerializeField, Tooltip("The distance from the target to follow from.")] 17 | private float _camDistance = -15; 18 | 19 | // Start is called before the first frame update 20 | void Start() 21 | { 22 | 23 | } 24 | 25 | // Update is called once per frame 26 | void Update() 27 | { 28 | if (!_camTarget) 29 | return; 30 | 31 | Vector3 targetPos = _camTarget.transform.position; 32 | targetPos += _targetOffset; 33 | 34 | targetPos.y += _camHeight; 35 | targetPos.z += _camDistance; 36 | 37 | // move camera towards target position 38 | Vector3 camPos = transform.position; 39 | transform.position = Vector3.Lerp( transform.position, targetPos, Time.deltaTime * 5.0f ); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Chapter06/FollowCam.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class FollowCam : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The object to follow.")] 8 | private GameObject _camTarget; 9 | 10 | [SerializeField, Tooltip("Target offset.")] 11 | private Vector3 _targetOffset; 12 | 13 | [SerializeField, Tooltip("The height off the ground to follow from.")] 14 | private float _camHeight = 10; 15 | 16 | [SerializeField, Tooltip("The distance from the target to follow from.")] 17 | private float _camDistance = -15; 18 | 19 | // Start is called before the first frame update 20 | void Start() 21 | { 22 | 23 | } 24 | 25 | // Update is called once per frame 26 | void Update() 27 | { 28 | if (!_camTarget) 29 | return; 30 | 31 | Vector3 targetPos = _camTarget.transform.position; 32 | targetPos += _targetOffset; 33 | 34 | targetPos.y += _camHeight; 35 | targetPos.z += _camDistance; 36 | 37 | // move camera towards target position 38 | Vector3 camPos = transform.position; 39 | transform.position = Vector3.Lerp( transform.position, targetPos, Time.deltaTime * 5.0f ); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Chapter07/FollowCam.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class FollowCam : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The object to follow.")] 8 | private GameObject _camTarget; 9 | 10 | [SerializeField, Tooltip("Target offset.")] 11 | private Vector3 _targetOffset; 12 | 13 | [SerializeField, Tooltip("The height off the ground to follow from.")] 14 | private float _camHeight = 10; 15 | 16 | [SerializeField, Tooltip("The distance from the target to follow from.")] 17 | private float _camDistance = -15; 18 | 19 | // Start is called before the first frame update 20 | void Start() 21 | { 22 | 23 | } 24 | 25 | // Update is called once per frame 26 | void Update() 27 | { 28 | if (!_camTarget) 29 | return; 30 | 31 | Vector3 targetPos = _camTarget.transform.position; 32 | targetPos += _targetOffset; 33 | 34 | targetPos.y += _camHeight; 35 | targetPos.z += _camDistance; 36 | 37 | // move camera towards target position 38 | Vector3 camPos = transform.position; 39 | transform.position = Vector3.Lerp( transform.position, targetPos, Time.deltaTime * 5.0f ); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Chapter08/FollowCam.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class FollowCam : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The object to follow.")] 8 | private GameObject _camTarget; 9 | 10 | [SerializeField, Tooltip("Target offset.")] 11 | private Vector3 _targetOffset; 12 | 13 | [SerializeField, Tooltip("The height off the ground to follow from.")] 14 | private float _camHeight = 10; 15 | 16 | [SerializeField, Tooltip("The distance from the target to follow from.")] 17 | private float _camDistance = -15; 18 | 19 | // Start is called before the first frame update 20 | void Start() 21 | { 22 | 23 | } 24 | 25 | // Update is called once per frame 26 | void Update() 27 | { 28 | if (!_camTarget) 29 | return; 30 | 31 | Vector3 targetPos = _camTarget.transform.position; 32 | targetPos += _targetOffset; 33 | 34 | targetPos.y += _camHeight; 35 | targetPos.z += _camDistance; 36 | 37 | // move camera towards target position 38 | Vector3 camPos = transform.position; 39 | transform.position = Vector3.Lerp( transform.position, targetPos, Time.deltaTime * 5.0f ); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Chapter09/FollowCam.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class FollowCam : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The object to follow.")] 8 | private GameObject _camTarget; 9 | 10 | [SerializeField, Tooltip("Target offset.")] 11 | private Vector3 _targetOffset; 12 | 13 | [SerializeField, Tooltip("The height off the ground to follow from.")] 14 | private float _camHeight = 10; 15 | 16 | [SerializeField, Tooltip("The distance from the target to follow from.")] 17 | private float _camDistance = -15; 18 | 19 | // Start is called before the first frame update 20 | void Start() 21 | { 22 | 23 | } 24 | 25 | // Update is called once per frame 26 | void Update() 27 | { 28 | if (!_camTarget) 29 | return; 30 | 31 | Vector3 targetPos = _camTarget.transform.position; 32 | targetPos += _targetOffset; 33 | 34 | targetPos.y += _camHeight; 35 | targetPos.z += _camDistance; 36 | 37 | // move camera towards target position 38 | Vector3 camPos = transform.position; 39 | transform.position = Vector3.Lerp( transform.position, targetPos, Time.deltaTime * 5.0f ); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Chapter10/FollowCam.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class FollowCam : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The object to follow.")] 8 | private GameObject _camTarget; 9 | 10 | [SerializeField, Tooltip("Target offset.")] 11 | private Vector3 _targetOffset; 12 | 13 | [SerializeField, Tooltip("The height off the ground to follow from.")] 14 | private float _camHeight = 10; 15 | 16 | [SerializeField, Tooltip("The distance from the target to follow from.")] 17 | private float _camDistance = -15; 18 | 19 | // Start is called before the first frame update 20 | void Start() 21 | { 22 | 23 | } 24 | 25 | // Update is called once per frame 26 | void Update() 27 | { 28 | if (!_camTarget) 29 | return; 30 | 31 | Vector3 targetPos = _camTarget.transform.position; 32 | targetPos += _targetOffset; 33 | 34 | targetPos.y += _camHeight; 35 | targetPos.z += _camDistance; 36 | 37 | // move camera towards target position 38 | Vector3 camPos = transform.position; 39 | transform.position = Vector3.Lerp( transform.position, targetPos, Time.deltaTime * 5.0f ); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Chapter11/FollowCam.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class FollowCam : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The object to follow.")] 8 | private GameObject _camTarget; 9 | 10 | [SerializeField, Tooltip("Target offset.")] 11 | private Vector3 _targetOffset; 12 | 13 | [SerializeField, Tooltip("The height off the ground to follow from.")] 14 | private float _camHeight = 10; 15 | 16 | [SerializeField, Tooltip("The distance from the target to follow from.")] 17 | private float _camDistance = -15; 18 | 19 | // Start is called before the first frame update 20 | void Start() 21 | { 22 | 23 | } 24 | 25 | // Update is called once per frame 26 | void Update() 27 | { 28 | if (!_camTarget) 29 | return; 30 | 31 | Vector3 targetPos = _camTarget.transform.position; 32 | targetPos += _targetOffset; 33 | 34 | targetPos.y += _camHeight; 35 | targetPos.z += _camDistance; 36 | 37 | // move camera towards target position 38 | Vector3 camPos = transform.position; 39 | transform.position = Vector3.Lerp( transform.position, targetPos, Time.deltaTime * 5.0f ); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Chapter12/FollowCam.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class FollowCam : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The object to follow.")] 8 | private GameObject _camTarget; 9 | 10 | [SerializeField, Tooltip("Target offset.")] 11 | private Vector3 _targetOffset; 12 | 13 | [SerializeField, Tooltip("The height off the ground to follow from.")] 14 | private float _camHeight = 10; 15 | 16 | [SerializeField, Tooltip("The distance from the target to follow from.")] 17 | private float _camDistance = -15; 18 | 19 | // Start is called before the first frame update 20 | void Start() 21 | { 22 | 23 | } 24 | 25 | // Update is called once per frame 26 | void Update() 27 | { 28 | if (!_camTarget) 29 | return; 30 | 31 | Vector3 targetPos = _camTarget.transform.position; 32 | targetPos += _targetOffset; 33 | 34 | targetPos.y += _camHeight; 35 | targetPos.z += _camDistance; 36 | 37 | // move camera towards target position 38 | Vector3 camPos = transform.position; 39 | transform.position = Vector3.Lerp( transform.position, targetPos, Time.deltaTime * 5.0f ); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Chapter13/FollowCam.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class FollowCam : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The object to follow.")] 8 | private GameObject _camTarget; 9 | 10 | [SerializeField, Tooltip("Target offset.")] 11 | private Vector3 _targetOffset; 12 | 13 | [SerializeField, Tooltip("The height off the ground to follow from.")] 14 | private float _camHeight = 10; 15 | 16 | [SerializeField, Tooltip("The distance from the target to follow from.")] 17 | private float _camDistance = -15; 18 | 19 | // Start is called before the first frame update 20 | void Start() 21 | { 22 | 23 | } 24 | 25 | // Update is called once per frame 26 | void Update() 27 | { 28 | if (!_camTarget) 29 | return; 30 | 31 | Vector3 targetPos = _camTarget.transform.position; 32 | targetPos += _targetOffset; 33 | 34 | targetPos.y += _camHeight; 35 | targetPos.z += _camDistance; 36 | 37 | // move camera towards target position 38 | Vector3 camPos = transform.position; 39 | transform.position = Vector3.Lerp( transform.position, targetPos, Time.deltaTime * 5.0f ); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Chapter14/FollowCam.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class FollowCam : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The object to follow.")] 8 | private GameObject _camTarget; 9 | 10 | [SerializeField, Tooltip("Target offset.")] 11 | private Vector3 _targetOffset; 12 | 13 | [SerializeField, Tooltip("The height off the ground to follow from.")] 14 | private float _camHeight = 10; 15 | 16 | [SerializeField, Tooltip("The distance from the target to follow from.")] 17 | private float _camDistance = -15; 18 | 19 | // Start is called before the first frame update 20 | void Start() 21 | { 22 | 23 | } 24 | 25 | // Update is called once per frame 26 | void Update() 27 | { 28 | if (!_camTarget) 29 | return; 30 | 31 | Vector3 targetPos = _camTarget.transform.position; 32 | targetPos += _targetOffset; 33 | 34 | targetPos.y += _camHeight; 35 | targetPos.z += _camDistance; 36 | 37 | // move camera towards target position 38 | Vector3 camPos = transform.position; 39 | transform.position = Vector3.Lerp( transform.position, targetPos, Time.deltaTime * 5.0f ); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Chapter04/MainGameHUD.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | 6 | using TMPro; 7 | 8 | public class MainGameHUD : MonoBehaviour 9 | { 10 | 11 | [SerializeField, Tooltip("TMP object displaying our current health.")] 12 | TextMeshProUGUI _healthValueText; 13 | 14 | [SerializeField, Tooltip("TMP object displaying the # of collected coins.")] 15 | TextMeshProUGUI _coinValueText; 16 | 17 | [SerializeField, Tooltip("TMP object displaying lives remaining.")] 18 | TextMeshProUGUI _livesValueText; 19 | 20 | [SerializeField, Tooltip("The Health Manager we're displaying data for.")] 21 | HealthManager _healthManager; 22 | 23 | 24 | // Start is called before the first frame update 25 | void Start() 26 | { 27 | 28 | } 29 | 30 | // Update is called once per frame 31 | void Update() 32 | { 33 | int curHealth = Mathf.RoundToInt(_healthManager.GetHealthCur()); 34 | int maxHealth = Mathf.RoundToInt(_healthManager.GetHealthMax()); 35 | _healthValueText.text = curHealth + "/" + maxHealth; 36 | 37 | _coinValueText.text = GameSessionManager.Instance.GetCoins().ToString(); 38 | 39 | _livesValueText.text = GameSessionManager.Instance.GetLives().ToString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter05/MainGameHUD.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | 6 | using TMPro; 7 | 8 | public class MainGameHUD : MonoBehaviour 9 | { 10 | 11 | [SerializeField, Tooltip("TMP object displaying our current health.")] 12 | TextMeshProUGUI _healthValueText; 13 | 14 | [SerializeField, Tooltip("TMP object displaying the # of collected coins.")] 15 | TextMeshProUGUI _coinValueText; 16 | 17 | [SerializeField, Tooltip("TMP object displaying lives remaining.")] 18 | TextMeshProUGUI _livesValueText; 19 | 20 | [SerializeField, Tooltip("The Health Manager we're displaying data for.")] 21 | HealthManager _healthManager; 22 | 23 | 24 | // Start is called before the first frame update 25 | void Start() 26 | { 27 | 28 | } 29 | 30 | // Update is called once per frame 31 | void Update() 32 | { 33 | int curHealth = Mathf.RoundToInt(_healthManager.GetHealthCur()); 34 | int maxHealth = Mathf.RoundToInt(_healthManager.GetHealthMax()); 35 | _healthValueText.text = curHealth + "/" + maxHealth; 36 | 37 | _coinValueText.text = GameSessionManager.Instance.GetCoins().ToString(); 38 | 39 | _livesValueText.text = GameSessionManager.Instance.GetLives().ToString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter06/MainGameHUD.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | 6 | using TMPro; 7 | 8 | public class MainGameHUD : MonoBehaviour 9 | { 10 | 11 | [SerializeField, Tooltip("TMP object displaying our current health.")] 12 | TextMeshProUGUI _healthValueText; 13 | 14 | [SerializeField, Tooltip("TMP object displaying the # of collected coins.")] 15 | TextMeshProUGUI _coinValueText; 16 | 17 | [SerializeField, Tooltip("TMP object displaying lives remaining.")] 18 | TextMeshProUGUI _livesValueText; 19 | 20 | [SerializeField, Tooltip("The Health Manager we're displaying data for.")] 21 | HealthManager _healthManager; 22 | 23 | 24 | // Start is called before the first frame update 25 | void Start() 26 | { 27 | 28 | } 29 | 30 | // Update is called once per frame 31 | void Update() 32 | { 33 | int curHealth = Mathf.RoundToInt(_healthManager.GetHealthCur()); 34 | int maxHealth = Mathf.RoundToInt(_healthManager.GetHealthMax()); 35 | _healthValueText.text = curHealth + "/" + maxHealth; 36 | 37 | _coinValueText.text = GameSessionManager.Instance.GetCoins().ToString(); 38 | 39 | _livesValueText.text = GameSessionManager.Instance.GetLives().ToString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter07/MainGameHUD.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | 6 | using TMPro; 7 | 8 | public class MainGameHUD : MonoBehaviour 9 | { 10 | 11 | [SerializeField, Tooltip("TMP object displaying our current health.")] 12 | TextMeshProUGUI _healthValueText; 13 | 14 | [SerializeField, Tooltip("TMP object displaying the # of collected coins.")] 15 | TextMeshProUGUI _coinValueText; 16 | 17 | [SerializeField, Tooltip("TMP object displaying lives remaining.")] 18 | TextMeshProUGUI _livesValueText; 19 | 20 | [SerializeField, Tooltip("The Health Manager we're displaying data for.")] 21 | HealthManager _healthManager; 22 | 23 | 24 | // Start is called before the first frame update 25 | void Start() 26 | { 27 | 28 | } 29 | 30 | // Update is called once per frame 31 | void Update() 32 | { 33 | int curHealth = Mathf.RoundToInt(_healthManager.GetHealthCur()); 34 | int maxHealth = Mathf.RoundToInt(_healthManager.GetHealthMax()); 35 | _healthValueText.text = curHealth + "/" + maxHealth; 36 | 37 | _coinValueText.text = GameSessionManager.Instance.GetCoins().ToString(); 38 | 39 | _livesValueText.text = GameSessionManager.Instance.GetLives().ToString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter08/MainGameHUD.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | 6 | using TMPro; 7 | 8 | public class MainGameHUD : MonoBehaviour 9 | { 10 | 11 | [SerializeField, Tooltip("TMP object displaying our current health.")] 12 | TextMeshProUGUI _healthValueText; 13 | 14 | [SerializeField, Tooltip("TMP object displaying the # of collected coins.")] 15 | TextMeshProUGUI _coinValueText; 16 | 17 | [SerializeField, Tooltip("TMP object displaying lives remaining.")] 18 | TextMeshProUGUI _livesValueText; 19 | 20 | [SerializeField, Tooltip("The Health Manager we're displaying data for.")] 21 | HealthManager _healthManager; 22 | 23 | 24 | // Start is called before the first frame update 25 | void Start() 26 | { 27 | 28 | } 29 | 30 | // Update is called once per frame 31 | void Update() 32 | { 33 | int curHealth = Mathf.RoundToInt(_healthManager.GetHealthCur()); 34 | int maxHealth = Mathf.RoundToInt(_healthManager.GetHealthMax()); 35 | _healthValueText.text = curHealth + "/" + maxHealth; 36 | 37 | _coinValueText.text = GameSessionManager.Instance.GetCoins().ToString(); 38 | 39 | _livesValueText.text = GameSessionManager.Instance.GetLives().ToString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter09/MainGameHUD.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | 6 | using TMPro; 7 | 8 | public class MainGameHUD : MonoBehaviour 9 | { 10 | 11 | [SerializeField, Tooltip("TMP object displaying our current health.")] 12 | TextMeshProUGUI _healthValueText; 13 | 14 | [SerializeField, Tooltip("TMP object displaying the # of collected coins.")] 15 | TextMeshProUGUI _coinValueText; 16 | 17 | [SerializeField, Tooltip("TMP object displaying lives remaining.")] 18 | TextMeshProUGUI _livesValueText; 19 | 20 | [SerializeField, Tooltip("The Health Manager we're displaying data for.")] 21 | HealthManager _healthManager; 22 | 23 | 24 | // Start is called before the first frame update 25 | void Start() 26 | { 27 | 28 | } 29 | 30 | // Update is called once per frame 31 | void Update() 32 | { 33 | int curHealth = Mathf.RoundToInt(_healthManager.GetHealthCur()); 34 | int maxHealth = Mathf.RoundToInt(_healthManager.GetHealthMax()); 35 | _healthValueText.text = curHealth + "/" + maxHealth; 36 | 37 | _coinValueText.text = GameSessionManager.Instance.GetCoins().ToString(); 38 | 39 | _livesValueText.text = GameSessionManager.Instance.GetLives().ToString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter10/MainGameHUD.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | 6 | using TMPro; 7 | 8 | public class MainGameHUD : MonoBehaviour 9 | { 10 | 11 | [SerializeField, Tooltip("TMP object displaying our current health.")] 12 | TextMeshProUGUI _healthValueText; 13 | 14 | [SerializeField, Tooltip("TMP object displaying the # of collected coins.")] 15 | TextMeshProUGUI _coinValueText; 16 | 17 | [SerializeField, Tooltip("TMP object displaying lives remaining.")] 18 | TextMeshProUGUI _livesValueText; 19 | 20 | [SerializeField, Tooltip("The Health Manager we're displaying data for.")] 21 | HealthManager _healthManager; 22 | 23 | 24 | // Start is called before the first frame update 25 | void Start() 26 | { 27 | 28 | } 29 | 30 | // Update is called once per frame 31 | void Update() 32 | { 33 | int curHealth = Mathf.RoundToInt(_healthManager.GetHealthCur()); 34 | int maxHealth = Mathf.RoundToInt(_healthManager.GetHealthMax()); 35 | _healthValueText.text = curHealth + "/" + maxHealth; 36 | 37 | _coinValueText.text = GameSessionManager.Instance.GetCoins().ToString(); 38 | 39 | _livesValueText.text = GameSessionManager.Instance.GetLives().ToString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter11/MainGameHUD.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | 6 | using TMPro; 7 | 8 | public class MainGameHUD : MonoBehaviour 9 | { 10 | 11 | [SerializeField, Tooltip("TMP object displaying our current health.")] 12 | TextMeshProUGUI _healthValueText; 13 | 14 | [SerializeField, Tooltip("TMP object displaying the # of collected coins.")] 15 | TextMeshProUGUI _coinValueText; 16 | 17 | [SerializeField, Tooltip("TMP object displaying lives remaining.")] 18 | TextMeshProUGUI _livesValueText; 19 | 20 | [SerializeField, Tooltip("The Health Manager we're displaying data for.")] 21 | HealthManager _healthManager; 22 | 23 | 24 | // Start is called before the first frame update 25 | void Start() 26 | { 27 | 28 | } 29 | 30 | // Update is called once per frame 31 | void Update() 32 | { 33 | int curHealth = Mathf.RoundToInt(_healthManager.GetHealthCur()); 34 | int maxHealth = Mathf.RoundToInt(_healthManager.GetHealthMax()); 35 | _healthValueText.text = curHealth + "/" + maxHealth; 36 | 37 | _coinValueText.text = GameSessionManager.Instance.GetCoins().ToString(); 38 | 39 | _livesValueText.text = GameSessionManager.Instance.GetLives().ToString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter12/MainGameHUD.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | 6 | using TMPro; 7 | 8 | public class MainGameHUD : MonoBehaviour 9 | { 10 | 11 | [SerializeField, Tooltip("TMP object displaying our current health.")] 12 | TextMeshProUGUI _healthValueText; 13 | 14 | [SerializeField, Tooltip("TMP object displaying the # of collected coins.")] 15 | TextMeshProUGUI _coinValueText; 16 | 17 | [SerializeField, Tooltip("TMP object displaying lives remaining.")] 18 | TextMeshProUGUI _livesValueText; 19 | 20 | [SerializeField, Tooltip("The Health Manager we're displaying data for.")] 21 | HealthManager _healthManager; 22 | 23 | 24 | // Start is called before the first frame update 25 | void Start() 26 | { 27 | 28 | } 29 | 30 | // Update is called once per frame 31 | void Update() 32 | { 33 | int curHealth = Mathf.RoundToInt(_healthManager.GetHealthCur()); 34 | int maxHealth = Mathf.RoundToInt(_healthManager.GetHealthMax()); 35 | _healthValueText.text = curHealth + "/" + maxHealth; 36 | 37 | _coinValueText.text = GameSessionManager.Instance.GetCoins().ToString(); 38 | 39 | _livesValueText.text = GameSessionManager.Instance.GetLives().ToString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter13/MainGameHUD.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | 6 | using TMPro; 7 | 8 | public class MainGameHUD : MonoBehaviour 9 | { 10 | 11 | [SerializeField, Tooltip("TMP object displaying our current health.")] 12 | TextMeshProUGUI _healthValueText; 13 | 14 | [SerializeField, Tooltip("TMP object displaying the # of collected coins.")] 15 | TextMeshProUGUI _coinValueText; 16 | 17 | [SerializeField, Tooltip("TMP object displaying lives remaining.")] 18 | TextMeshProUGUI _livesValueText; 19 | 20 | [SerializeField, Tooltip("The Health Manager we're displaying data for.")] 21 | HealthManager _healthManager; 22 | 23 | 24 | // Start is called before the first frame update 25 | void Start() 26 | { 27 | 28 | } 29 | 30 | // Update is called once per frame 31 | void Update() 32 | { 33 | int curHealth = Mathf.RoundToInt(_healthManager.GetHealthCur()); 34 | int maxHealth = Mathf.RoundToInt(_healthManager.GetHealthMax()); 35 | _healthValueText.text = curHealth + "/" + maxHealth; 36 | 37 | _coinValueText.text = GameSessionManager.Instance.GetCoins().ToString(); 38 | 39 | _livesValueText.text = GameSessionManager.Instance.GetLives().ToString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter14/MainGameHUD.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.UI; 5 | 6 | using TMPro; 7 | 8 | public class MainGameHUD : MonoBehaviour 9 | { 10 | 11 | [SerializeField, Tooltip("TMP object displaying our current health.")] 12 | TextMeshProUGUI _healthValueText; 13 | 14 | [SerializeField, Tooltip("TMP object displaying the # of collected coins.")] 15 | TextMeshProUGUI _coinValueText; 16 | 17 | [SerializeField, Tooltip("TMP object displaying lives remaining.")] 18 | TextMeshProUGUI _livesValueText; 19 | 20 | [SerializeField, Tooltip("The Health Manager we're displaying data for.")] 21 | HealthManager _healthManager; 22 | 23 | 24 | // Start is called before the first frame update 25 | void Start() 26 | { 27 | 28 | } 29 | 30 | // Update is called once per frame 31 | void Update() 32 | { 33 | int curHealth = Mathf.RoundToInt(_healthManager.GetHealthCur()); 34 | int maxHealth = Mathf.RoundToInt(_healthManager.GetHealthMax()); 35 | _healthValueText.text = curHealth + "/" + maxHealth; 36 | 37 | _coinValueText.text = GameSessionManager.Instance.GetCoins().ToString(); 38 | 39 | _livesValueText.text = GameSessionManager.Instance.GetLives().ToString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter02/ItemSpawnZone.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class ItemSpawnZone : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn in this zone.")] 8 | private GameObject _itemToSpawn; 9 | 10 | [SerializeField, Tooltip("Number of items to spawn.")] 11 | private float _itemCount = 30; 12 | 13 | [SerializeField, Tooltip("The area to spawn these items.")] 14 | private BoxCollider _spawnZone; 15 | 16 | void Start() 17 | { 18 | // spawn the items within this area 19 | for (int i = 0; i < _itemCount; i++) 20 | { 21 | SpawnItemAtRandomPosition(); 22 | } 23 | } 24 | 25 | void SpawnItemAtRandomPosition() 26 | { 27 | Vector3 randomPos; 28 | 29 | // randomize location based on the size of the associated BoxCollider 30 | randomPos.x = Random.Range(_spawnZone.bounds.min.x, 31 | _spawnZone.bounds.max.x); 32 | 33 | randomPos.y = Random.Range(_spawnZone.bounds.min.y, 34 | _spawnZone.bounds.max.y); 35 | 36 | randomPos.z = Random.Range(_spawnZone.bounds.min.z, _spawnZone.bounds.max.z); 37 | 38 | // spawn the item prefab at this position 39 | Instantiate(_itemToSpawn, randomPos, Quaternion.identity); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter03/ItemSpawnZone.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class ItemSpawnZone : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn in this zone.")] 8 | private GameObject _itemToSpawn; 9 | 10 | [SerializeField, Tooltip("Number of items to spawn.")] 11 | private float _itemCount = 30; 12 | 13 | [SerializeField, Tooltip("The area to spawn these items.")] 14 | private BoxCollider _spawnZone; 15 | 16 | void Start() 17 | { 18 | // spawn the items within this area 19 | for (int i = 0; i < _itemCount; i++) 20 | { 21 | SpawnItemAtRandomPosition(); 22 | } 23 | } 24 | 25 | void SpawnItemAtRandomPosition() 26 | { 27 | Vector3 randomPos; 28 | 29 | // randomize location based on the size of the associated BoxCollider 30 | randomPos.x = Random.Range(_spawnZone.bounds.min.x, 31 | _spawnZone.bounds.max.x); 32 | 33 | randomPos.y = Random.Range(_spawnZone.bounds.min.y, 34 | _spawnZone.bounds.max.y); 35 | 36 | randomPos.z = Random.Range(_spawnZone.bounds.min.z, _spawnZone.bounds.max.z); 37 | 38 | // spawn the item prefab at this position 39 | Instantiate(_itemToSpawn, randomPos, Quaternion.identity); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter04/ItemSpawnZone.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class ItemSpawnZone : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn in this zone.")] 8 | private GameObject _itemToSpawn; 9 | 10 | [SerializeField, Tooltip("Number of items to spawn.")] 11 | private float _itemCount = 30; 12 | 13 | [SerializeField, Tooltip("The area to spawn these items.")] 14 | private BoxCollider _spawnZone; 15 | 16 | void Start() 17 | { 18 | // spawn the items within this area 19 | for (int i = 0; i < _itemCount; i++) 20 | { 21 | SpawnItemAtRandomPosition(); 22 | } 23 | } 24 | 25 | void SpawnItemAtRandomPosition() 26 | { 27 | Vector3 randomPos; 28 | 29 | // randomize location based on the size of the associated BoxCollider 30 | randomPos.x = Random.Range(_spawnZone.bounds.min.x, 31 | _spawnZone.bounds.max.x); 32 | 33 | randomPos.y = Random.Range(_spawnZone.bounds.min.y, 34 | _spawnZone.bounds.max.y); 35 | 36 | randomPos.z = Random.Range(_spawnZone.bounds.min.z, _spawnZone.bounds.max.z); 37 | 38 | // spawn the item prefab at this position 39 | Instantiate(_itemToSpawn, randomPos, Quaternion.identity); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter14/SwitchSceneOnCollision.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class SwitchSceneOnCollision : MonoBehaviour 7 | { 8 | [SerializeField, Tooltip("Name of scene to load.")] 9 | private string _sceneToLoad; 10 | 11 | [SerializeField, Tooltip("Seconds between collision and load.")] 12 | private float _transitionTime = 1f; 13 | 14 | private bool _hasCollided = false; 15 | 16 | void Update() 17 | { 18 | if (_hasCollided ) 19 | { 20 | _transitionTime -= Time.deltaTime; 21 | if (_transitionTime <= 0f) 22 | { 23 | // time to load the scene 24 | // remember: this scene needs to be added 25 | // to the Build Settings 'Scenes' list ! 26 | //SceneManager.LoadScene(_sceneToLoad, LoadSceneMode.Single); 27 | SceneTransition.TransitionToScene( _sceneToLoad ); 28 | enabled = false; 29 | } 30 | } 31 | } 32 | 33 | void OnTriggerEnter(Collider collider) 34 | { 35 | if (collider.gameObject.GetComponent() ) 36 | { 37 | // the player has collided with this obj 38 | _hasCollided = true; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter01/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter02/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter03/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter04/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter05/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter06/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter07/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter08/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter09/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter10/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter11/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter12/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter13/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter14/GroundObjectController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GroundObjectController : MonoBehaviour 6 | { 7 | // Start is called before the first frame update 8 | void Start() 9 | { 10 | 11 | } 12 | 13 | // Update is called once per frame 14 | void Update() 15 | { 16 | // how many units rotation will change for every second a key is held 17 | float speed = 15f; 18 | 19 | // this gets the current rotation of the ground object 20 | // based on the keys pressed, we will be altering this value and re-applying it 21 | Vector3 newRotation = transform.localEulerAngles; 22 | 23 | // go through each of the direction keys: up, down, left, right 24 | // if any of them are pressed, alter the rotation of the ground plane 25 | if (Input.GetKey(KeyCode.RightArrow)) 26 | newRotation.z += Time.deltaTime * speed; 27 | 28 | if (Input.GetKey(KeyCode.LeftArrow)) 29 | newRotation.z -= Time.deltaTime * speed; 30 | 31 | if (Input.GetKey(KeyCode.UpArrow)) 32 | newRotation.x += Time.deltaTime * speed; 33 | 34 | if (Input.GetKey(KeyCode.DownArrow)) 35 | newRotation.x -= Time.deltaTime * speed; 36 | 37 | // apply the altered rotation to the ground object 38 | transform.localEulerAngles = newRotation; 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter05/VFXHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class VFXHandler : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn when hit and destroyed.")] 8 | GameObject _mainExplosionChunk; 9 | 10 | [SerializeField, Tooltip("Less common prefab when hit and destroyed.")] 11 | GameObject _secondaryExplosionChunk; 12 | 13 | [SerializeField, Tooltip("Min explosion chunks to spawn.")] 14 | int _minChunks; 15 | 16 | [SerializeField, Tooltip("Max amount to spawn.")] 17 | int _maxChunks; 18 | 19 | [SerializeField, Tooltip("Force of explosion.")] 20 | float _explosionForce = 1; 21 | 22 | public void SpawnExplosion() 23 | { 24 | // spawn a random number of the main chunks 25 | int rand = Random.Range(_minChunks, _maxChunks); 26 | if (_mainExplosionChunk) 27 | for ( int i = 0; i < rand; i++ ) 28 | SpawnSubObject( _mainExplosionChunk ); 29 | 30 | // now spawn the secondary object 31 | // (but only half the amount) 32 | rand /= 2; 33 | if (_secondaryExplosionChunk) 34 | for (int i = 0; i < rand; i++) 35 | SpawnSubObject(_secondaryExplosionChunk); 36 | } 37 | 38 | void SpawnSubObject( GameObject prefab ) 39 | { 40 | // get a random point around our object 41 | // should prevent collision with parent 42 | Vector3 pos = transform.position; 43 | pos += Random.onUnitSphere * 0.8f; 44 | 45 | GameObject newObj = Instantiate(prefab, pos, Quaternion.identity); 46 | 47 | // give the chunk a random velocity 48 | Rigidbody rb = newObj.GetComponent(); 49 | rb?.AddExplosionForce(_explosionForce, transform.position, 1f); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Chapter06/VFXHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class VFXHandler : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn when hit and destroyed.")] 8 | GameObject _mainExplosionChunk; 9 | 10 | [SerializeField, Tooltip("Less common prefab when hit and destroyed.")] 11 | GameObject _secondaryExplosionChunk; 12 | 13 | [SerializeField, Tooltip("Min explosion chunks to spawn.")] 14 | int _minChunks; 15 | 16 | [SerializeField, Tooltip("Max amount to spawn.")] 17 | int _maxChunks; 18 | 19 | [SerializeField, Tooltip("Force of explosion.")] 20 | float _explosionForce = 1; 21 | 22 | public void SpawnExplosion() 23 | { 24 | // spawn a random number of the main chunks 25 | int rand = Random.Range(_minChunks, _maxChunks); 26 | if (_mainExplosionChunk) 27 | for ( int i = 0; i < rand; i++ ) 28 | SpawnSubObject( _mainExplosionChunk ); 29 | 30 | // now spawn the secondary object 31 | // (but only half the amount) 32 | rand /= 2; 33 | if (_secondaryExplosionChunk) 34 | for (int i = 0; i < rand; i++) 35 | SpawnSubObject(_secondaryExplosionChunk); 36 | } 37 | 38 | void SpawnSubObject( GameObject prefab ) 39 | { 40 | // get a random point around our object 41 | // should prevent collision with parent 42 | Vector3 pos = transform.position; 43 | pos += Random.onUnitSphere * 0.8f; 44 | 45 | GameObject newObj = Instantiate(prefab, pos, Quaternion.identity); 46 | 47 | // give the chunk a random velocity 48 | Rigidbody rb = newObj.GetComponent(); 49 | rb?.AddExplosionForce(_explosionForce, transform.position, 1f); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Chapter07/VFXHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class VFXHandler : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn when hit and destroyed.")] 8 | GameObject _mainExplosionChunk; 9 | 10 | [SerializeField, Tooltip("Less common prefab when hit and destroyed.")] 11 | GameObject _secondaryExplosionChunk; 12 | 13 | [SerializeField, Tooltip("Min explosion chunks to spawn.")] 14 | int _minChunks; 15 | 16 | [SerializeField, Tooltip("Max amount to spawn.")] 17 | int _maxChunks; 18 | 19 | [SerializeField, Tooltip("Force of explosion.")] 20 | float _explosionForce = 1; 21 | 22 | public void SpawnExplosion() 23 | { 24 | // spawn a random number of the main chunks 25 | int rand = Random.Range(_minChunks, _maxChunks); 26 | if (_mainExplosionChunk) 27 | for ( int i = 0; i < rand; i++ ) 28 | SpawnSubObject( _mainExplosionChunk ); 29 | 30 | // now spawn the secondary object 31 | // (but only half the amount) 32 | rand /= 2; 33 | if (_secondaryExplosionChunk) 34 | for (int i = 0; i < rand; i++) 35 | SpawnSubObject(_secondaryExplosionChunk); 36 | } 37 | 38 | void SpawnSubObject( GameObject prefab ) 39 | { 40 | // get a random point around our object 41 | // should prevent collision with parent 42 | Vector3 pos = transform.position; 43 | pos += Random.onUnitSphere * 0.8f; 44 | 45 | GameObject newObj = Instantiate(prefab, pos, Quaternion.identity); 46 | 47 | // give the chunk a random velocity 48 | Rigidbody rb = newObj.GetComponent(); 49 | rb?.AddExplosionForce(_explosionForce, transform.position, 1f); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Chapter08/VFXHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class VFXHandler : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn when hit and destroyed.")] 8 | GameObject _mainExplosionChunk; 9 | 10 | [SerializeField, Tooltip("Less common prefab when hit and destroyed.")] 11 | GameObject _secondaryExplosionChunk; 12 | 13 | [SerializeField, Tooltip("Min explosion chunks to spawn.")] 14 | int _minChunks; 15 | 16 | [SerializeField, Tooltip("Max amount to spawn.")] 17 | int _maxChunks; 18 | 19 | [SerializeField, Tooltip("Force of explosion.")] 20 | float _explosionForce = 1; 21 | 22 | public void SpawnExplosion() 23 | { 24 | // spawn a random number of the main chunks 25 | int rand = Random.Range(_minChunks, _maxChunks); 26 | if (_mainExplosionChunk) 27 | for ( int i = 0; i < rand; i++ ) 28 | SpawnSubObject( _mainExplosionChunk ); 29 | 30 | // now spawn the secondary object 31 | // (but only half the amount) 32 | rand /= 2; 33 | if (_secondaryExplosionChunk) 34 | for (int i = 0; i < rand; i++) 35 | SpawnSubObject(_secondaryExplosionChunk); 36 | } 37 | 38 | void SpawnSubObject( GameObject prefab ) 39 | { 40 | // get a random point around our object 41 | // should prevent collision with parent 42 | Vector3 pos = transform.position; 43 | pos += Random.onUnitSphere * 0.8f; 44 | 45 | GameObject newObj = Instantiate(prefab, pos, Quaternion.identity); 46 | 47 | // give the chunk a random velocity 48 | Rigidbody rb = newObj.GetComponent(); 49 | rb?.AddExplosionForce(_explosionForce, transform.position, 1f); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Chapter09/VFXHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class VFXHandler : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn when hit and destroyed.")] 8 | GameObject _mainExplosionChunk; 9 | 10 | [SerializeField, Tooltip("Less common prefab when hit and destroyed.")] 11 | GameObject _secondaryExplosionChunk; 12 | 13 | [SerializeField, Tooltip("Min explosion chunks to spawn.")] 14 | int _minChunks; 15 | 16 | [SerializeField, Tooltip("Max amount to spawn.")] 17 | int _maxChunks; 18 | 19 | [SerializeField, Tooltip("Force of explosion.")] 20 | float _explosionForce = 1; 21 | 22 | public void SpawnExplosion() 23 | { 24 | // spawn a random number of the main chunks 25 | int rand = Random.Range(_minChunks, _maxChunks); 26 | if (_mainExplosionChunk) 27 | for ( int i = 0; i < rand; i++ ) 28 | SpawnSubObject( _mainExplosionChunk ); 29 | 30 | // now spawn the secondary object 31 | // (but only half the amount) 32 | rand /= 2; 33 | if (_secondaryExplosionChunk) 34 | for (int i = 0; i < rand; i++) 35 | SpawnSubObject(_secondaryExplosionChunk); 36 | } 37 | 38 | void SpawnSubObject( GameObject prefab ) 39 | { 40 | // get a random point around our object 41 | // should prevent collision with parent 42 | Vector3 pos = transform.position; 43 | pos += Random.onUnitSphere * 0.8f; 44 | 45 | GameObject newObj = Instantiate(prefab, pos, Quaternion.identity); 46 | 47 | // give the chunk a random velocity 48 | Rigidbody rb = newObj.GetComponent(); 49 | rb?.AddExplosionForce(_explosionForce, transform.position, 1f); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Chapter10/VFXHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class VFXHandler : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn when hit and destroyed.")] 8 | GameObject _mainExplosionChunk; 9 | 10 | [SerializeField, Tooltip("Less common prefab when hit and destroyed.")] 11 | GameObject _secondaryExplosionChunk; 12 | 13 | [SerializeField, Tooltip("Min explosion chunks to spawn.")] 14 | int _minChunks; 15 | 16 | [SerializeField, Tooltip("Max amount to spawn.")] 17 | int _maxChunks; 18 | 19 | [SerializeField, Tooltip("Force of explosion.")] 20 | float _explosionForce = 1; 21 | 22 | public void SpawnExplosion() 23 | { 24 | // spawn a random number of the main chunks 25 | int rand = Random.Range(_minChunks, _maxChunks); 26 | if (_mainExplosionChunk) 27 | for ( int i = 0; i < rand; i++ ) 28 | SpawnSubObject( _mainExplosionChunk ); 29 | 30 | // now spawn the secondary object 31 | // (but only half the amount) 32 | rand /= 2; 33 | if (_secondaryExplosionChunk) 34 | for (int i = 0; i < rand; i++) 35 | SpawnSubObject(_secondaryExplosionChunk); 36 | } 37 | 38 | void SpawnSubObject( GameObject prefab ) 39 | { 40 | // get a random point around our object 41 | // should prevent collision with parent 42 | Vector3 pos = transform.position; 43 | pos += Random.onUnitSphere * 0.8f; 44 | 45 | GameObject newObj = Instantiate(prefab, pos, Quaternion.identity); 46 | 47 | // give the chunk a random velocity 48 | Rigidbody rb = newObj.GetComponent(); 49 | rb?.AddExplosionForce(_explosionForce, transform.position, 1f); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Chapter11/VFXHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class VFXHandler : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn when hit and destroyed.")] 8 | GameObject _mainExplosionChunk; 9 | 10 | [SerializeField, Tooltip("Less common prefab when hit and destroyed.")] 11 | GameObject _secondaryExplosionChunk; 12 | 13 | [SerializeField, Tooltip("Min explosion chunks to spawn.")] 14 | int _minChunks; 15 | 16 | [SerializeField, Tooltip("Max amount to spawn.")] 17 | int _maxChunks; 18 | 19 | [SerializeField, Tooltip("Force of explosion.")] 20 | float _explosionForce = 1; 21 | 22 | public void SpawnExplosion() 23 | { 24 | // spawn a random number of the main chunks 25 | int rand = Random.Range(_minChunks, _maxChunks); 26 | if (_mainExplosionChunk) 27 | for ( int i = 0; i < rand; i++ ) 28 | SpawnSubObject( _mainExplosionChunk ); 29 | 30 | // now spawn the secondary object 31 | // (but only half the amount) 32 | rand /= 2; 33 | if (_secondaryExplosionChunk) 34 | for (int i = 0; i < rand; i++) 35 | SpawnSubObject(_secondaryExplosionChunk); 36 | } 37 | 38 | void SpawnSubObject( GameObject prefab ) 39 | { 40 | // get a random point around our object 41 | // should prevent collision with parent 42 | Vector3 pos = transform.position; 43 | pos += Random.onUnitSphere * 0.8f; 44 | 45 | GameObject newObj = Instantiate(prefab, pos, Quaternion.identity); 46 | 47 | // give the chunk a random velocity 48 | Rigidbody rb = newObj.GetComponent(); 49 | rb?.AddExplosionForce(_explosionForce, transform.position, 1f); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Chapter12/VFXHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class VFXHandler : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn when hit and destroyed.")] 8 | GameObject _mainExplosionChunk; 9 | 10 | [SerializeField, Tooltip("Less common prefab when hit and destroyed.")] 11 | GameObject _secondaryExplosionChunk; 12 | 13 | [SerializeField, Tooltip("Min explosion chunks to spawn.")] 14 | int _minChunks; 15 | 16 | [SerializeField, Tooltip("Max amount to spawn.")] 17 | int _maxChunks; 18 | 19 | [SerializeField, Tooltip("Force of explosion.")] 20 | float _explosionForce = 1; 21 | 22 | public void SpawnExplosion() 23 | { 24 | // spawn a random number of the main chunks 25 | int rand = Random.Range(_minChunks, _maxChunks); 26 | if (_mainExplosionChunk) 27 | for ( int i = 0; i < rand; i++ ) 28 | SpawnSubObject( _mainExplosionChunk ); 29 | 30 | // now spawn the secondary object 31 | // (but only half the amount) 32 | rand /= 2; 33 | if (_secondaryExplosionChunk) 34 | for (int i = 0; i < rand; i++) 35 | SpawnSubObject(_secondaryExplosionChunk); 36 | } 37 | 38 | void SpawnSubObject( GameObject prefab ) 39 | { 40 | // get a random point around our object 41 | // should prevent collision with parent 42 | Vector3 pos = transform.position; 43 | pos += Random.onUnitSphere * 0.8f; 44 | 45 | GameObject newObj = Instantiate(prefab, pos, Quaternion.identity); 46 | 47 | // give the chunk a random velocity 48 | Rigidbody rb = newObj.GetComponent(); 49 | rb?.AddExplosionForce(_explosionForce, transform.position, 1f); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Chapter13/VFXHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class VFXHandler : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn when hit and destroyed.")] 8 | GameObject _mainExplosionChunk; 9 | 10 | [SerializeField, Tooltip("Less common prefab when hit and destroyed.")] 11 | GameObject _secondaryExplosionChunk; 12 | 13 | [SerializeField, Tooltip("Min explosion chunks to spawn.")] 14 | int _minChunks; 15 | 16 | [SerializeField, Tooltip("Max amount to spawn.")] 17 | int _maxChunks; 18 | 19 | [SerializeField, Tooltip("Force of explosion.")] 20 | float _explosionForce = 1; 21 | 22 | public void SpawnExplosion() 23 | { 24 | // spawn a random number of the main chunks 25 | int rand = Random.Range(_minChunks, _maxChunks); 26 | if (_mainExplosionChunk) 27 | for ( int i = 0; i < rand; i++ ) 28 | SpawnSubObject( _mainExplosionChunk ); 29 | 30 | // now spawn the secondary object 31 | // (but only half the amount) 32 | rand /= 2; 33 | if (_secondaryExplosionChunk) 34 | for (int i = 0; i < rand; i++) 35 | SpawnSubObject(_secondaryExplosionChunk); 36 | } 37 | 38 | void SpawnSubObject( GameObject prefab ) 39 | { 40 | // get a random point around our object 41 | // should prevent collision with parent 42 | Vector3 pos = transform.position; 43 | pos += Random.onUnitSphere * 0.8f; 44 | 45 | GameObject newObj = Instantiate(prefab, pos, Quaternion.identity); 46 | 47 | // give the chunk a random velocity 48 | Rigidbody rb = newObj.GetComponent(); 49 | rb?.AddExplosionForce(_explosionForce, transform.position, 1f); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Chapter14/VFXHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class VFXHandler : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Prefab to spawn when hit and destroyed.")] 8 | GameObject _mainExplosionChunk; 9 | 10 | [SerializeField, Tooltip("Less common prefab when hit and destroyed.")] 11 | GameObject _secondaryExplosionChunk; 12 | 13 | [SerializeField, Tooltip("Min explosion chunks to spawn.")] 14 | int _minChunks; 15 | 16 | [SerializeField, Tooltip("Max amount to spawn.")] 17 | int _maxChunks; 18 | 19 | [SerializeField, Tooltip("Force of explosion.")] 20 | float _explosionForce = 1; 21 | 22 | public void SpawnExplosion() 23 | { 24 | // spawn a random number of the main chunks 25 | int rand = Random.Range(_minChunks, _maxChunks); 26 | if (_mainExplosionChunk) 27 | for ( int i = 0; i < rand; i++ ) 28 | SpawnSubObject( _mainExplosionChunk ); 29 | 30 | // now spawn the secondary object 31 | // (but only half the amount) 32 | rand /= 2; 33 | if (_secondaryExplosionChunk) 34 | for (int i = 0; i < rand; i++) 35 | SpawnSubObject(_secondaryExplosionChunk); 36 | } 37 | 38 | void SpawnSubObject( GameObject prefab ) 39 | { 40 | // get a random point around our object 41 | // should prevent collision with parent 42 | Vector3 pos = transform.position; 43 | pos += Random.onUnitSphere * 0.8f; 44 | 45 | GameObject newObj = Instantiate(prefab, pos, Quaternion.identity); 46 | 47 | // give the chunk a random velocity 48 | Rigidbody rb = newObj.GetComponent(); 49 | rb?.AddExplosionForce(_explosionForce, transform.position, 1f); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Chapter09/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | 16 | } 17 | 18 | // Update is called once per frame 19 | void Update() 20 | { 21 | // apply rotation 22 | Vector3 newRotation = transform.eulerAngles; 23 | newRotation.y += (_rotationSpeed * Time.deltaTime); 24 | transform.eulerAngles = newRotation; 25 | } 26 | 27 | public void onPickedUp( GameObject whoPickedUp ) 28 | { 29 | 30 | // NEW CODE 31 | if ( GetComponent() != null ) 32 | { 33 | PlayerController player = whoPickedUp.GetComponent(); 34 | if ( player != null ) 35 | { 36 | // player has picked up a weapon 37 | player.EquipWeapon(GetComponent()); 38 | 39 | // diable this 'pickup' script 40 | enabled = false; 41 | } 42 | return; 43 | } 44 | // --------- 45 | 46 | // show the collection count in the console window 47 | s_objectsCollected++; 48 | Debug.Log(s_objectsCollected + " items picked up."); 49 | 50 | // destroy the item 51 | Destroy(gameObject); 52 | 53 | 54 | 55 | // OLD 56 | // show the collection count in the console window 57 | s_objectsCollected++; 58 | Debug.Log(s_objectsCollected + " items picked up."); 59 | 60 | // destroy the item 61 | Destroy(gameObject); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Chapter10/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | 16 | } 17 | 18 | // Update is called once per frame 19 | void Update() 20 | { 21 | // apply rotation 22 | Vector3 newRotation = transform.eulerAngles; 23 | newRotation.y += (_rotationSpeed * Time.deltaTime); 24 | transform.eulerAngles = newRotation; 25 | } 26 | 27 | public void onPickedUp( GameObject whoPickedUp ) 28 | { 29 | // play soundFX 30 | SpawnedSoundFX.Spawn( transform.position ); 31 | 32 | if ( GetComponent() != null ) 33 | { 34 | PlayerController player = whoPickedUp.GetComponent(); 35 | if ( player != null ) 36 | { 37 | // player has picked up a weapon 38 | player.EquipWeapon(GetComponent()); 39 | 40 | // diable this 'pickup' script 41 | enabled = false; 42 | } 43 | return; 44 | } 45 | 46 | // show the collection count in the console window 47 | s_objectsCollected++; 48 | Debug.Log(s_objectsCollected + " items picked up."); 49 | 50 | // destroy the item 51 | Destroy(gameObject); 52 | 53 | 54 | 55 | // OLD 56 | // show the collection count in the console window 57 | s_objectsCollected++; 58 | Debug.Log(s_objectsCollected + " items picked up."); 59 | 60 | // destroy the item 61 | Destroy(gameObject); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Chapter11/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | 16 | } 17 | 18 | // Update is called once per frame 19 | void Update() 20 | { 21 | // apply rotation 22 | Vector3 newRotation = transform.eulerAngles; 23 | newRotation.y += (_rotationSpeed * Time.deltaTime); 24 | transform.eulerAngles = newRotation; 25 | } 26 | 27 | public void onPickedUp( GameObject whoPickedUp ) 28 | { 29 | // play soundFX 30 | SpawnedSoundFX.Spawn( transform.position ); 31 | 32 | if ( GetComponent() != null ) 33 | { 34 | PlayerController player = whoPickedUp.GetComponent(); 35 | if ( player != null ) 36 | { 37 | // player has picked up a weapon 38 | player.EquipWeapon(GetComponent()); 39 | 40 | // diable this 'pickup' script 41 | enabled = false; 42 | } 43 | return; 44 | } 45 | 46 | // show the collection count in the console window 47 | s_objectsCollected++; 48 | Debug.Log(s_objectsCollected + " items picked up."); 49 | 50 | // destroy the item 51 | Destroy(gameObject); 52 | 53 | 54 | 55 | // OLD 56 | // show the collection count in the console window 57 | s_objectsCollected++; 58 | Debug.Log(s_objectsCollected + " items picked up."); 59 | 60 | // destroy the item 61 | Destroy(gameObject); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Chapter12/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | 16 | } 17 | 18 | // Update is called once per frame 19 | void Update() 20 | { 21 | // apply rotation 22 | Vector3 newRotation = transform.eulerAngles; 23 | newRotation.y += (_rotationSpeed * Time.deltaTime); 24 | transform.eulerAngles = newRotation; 25 | } 26 | 27 | public void onPickedUp( GameObject whoPickedUp ) 28 | { 29 | // play soundFX 30 | SpawnedSoundFX.Spawn( transform.position ); 31 | 32 | if ( GetComponent() != null ) 33 | { 34 | PlayerController player = whoPickedUp.GetComponent(); 35 | if ( player != null ) 36 | { 37 | // player has picked up a weapon 38 | player.EquipWeapon(GetComponent()); 39 | 40 | // diable this 'pickup' script 41 | enabled = false; 42 | } 43 | return; 44 | } 45 | 46 | // show the collection count in the console window 47 | s_objectsCollected++; 48 | Debug.Log(s_objectsCollected + " items picked up."); 49 | 50 | // destroy the item 51 | Destroy(gameObject); 52 | 53 | 54 | 55 | // OLD 56 | // show the collection count in the console window 57 | s_objectsCollected++; 58 | Debug.Log(s_objectsCollected + " items picked up."); 59 | 60 | // destroy the item 61 | Destroy(gameObject); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Chapter13/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | 16 | } 17 | 18 | // Update is called once per frame 19 | void Update() 20 | { 21 | // apply rotation 22 | Vector3 newRotation = transform.eulerAngles; 23 | newRotation.y += (_rotationSpeed * Time.deltaTime); 24 | transform.eulerAngles = newRotation; 25 | } 26 | 27 | public void onPickedUp( GameObject whoPickedUp ) 28 | { 29 | // play soundFX 30 | SpawnedSoundFX.Spawn( transform.position ); 31 | 32 | if ( GetComponent() != null ) 33 | { 34 | PlayerController player = whoPickedUp.GetComponent(); 35 | if ( player != null ) 36 | { 37 | // player has picked up a weapon 38 | player.EquipWeapon(GetComponent()); 39 | 40 | // diable this 'pickup' script 41 | enabled = false; 42 | } 43 | return; 44 | } 45 | 46 | // show the collection count in the console window 47 | s_objectsCollected++; 48 | Debug.Log(s_objectsCollected + " items picked up."); 49 | 50 | // destroy the item 51 | Destroy(gameObject); 52 | 53 | 54 | 55 | // OLD 56 | // show the collection count in the console window 57 | s_objectsCollected++; 58 | Debug.Log(s_objectsCollected + " items picked up."); 59 | 60 | // destroy the item 61 | Destroy(gameObject); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Chapter14/PickUpItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class PickUpItem : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("The speed that this object rotates at.")] 8 | private float _rotationSpeed = 5; 9 | 10 | public static int s_objectsCollected = 0; 11 | 12 | // Start is called before the first frame update 13 | void Start() 14 | { 15 | 16 | } 17 | 18 | // Update is called once per frame 19 | void Update() 20 | { 21 | // apply rotation 22 | Vector3 newRotation = transform.eulerAngles; 23 | newRotation.y += (_rotationSpeed * Time.deltaTime); 24 | transform.eulerAngles = newRotation; 25 | } 26 | 27 | public void onPickedUp( GameObject whoPickedUp ) 28 | { 29 | // play soundFX 30 | SpawnedSoundFX.Spawn( transform.position ); 31 | 32 | if ( GetComponent() != null ) 33 | { 34 | PlayerController player = whoPickedUp.GetComponent(); 35 | if ( player != null ) 36 | { 37 | // player has picked up a weapon 38 | player.EquipWeapon(GetComponent()); 39 | 40 | // diable this 'pickup' script 41 | enabled = false; 42 | } 43 | return; 44 | } 45 | 46 | // show the collection count in the console window 47 | s_objectsCollected++; 48 | Debug.Log(s_objectsCollected + " items picked up."); 49 | 50 | // destroy the item 51 | Destroy(gameObject); 52 | 53 | 54 | 55 | // OLD 56 | // show the collection count in the console window 57 | s_objectsCollected++; 58 | Debug.Log(s_objectsCollected + " items picked up."); 59 | 60 | // destroy the item 61 | Destroy(gameObject); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mastering Game Design with Unity 2021 2 | 3 | Learn and Leverage the Power of Unity to Create Amazing Video Games! 4 | 5 | This is the repository for [Mastering Game Design with Unity 2021 6 | ](https://bpbonline.com/products/mastering-game-design-with-unity-2021),published by BPB Publications. 7 | 8 | ## About the Book 9 | The Unity Engine has been steadily evolving over the past few years into one of the most powerful resources for the game development community. Its feature-rich toolkit and user-friendliness make it an ideal foundation for budding game developers. 10 | 11 | The book ‘Mastering Game Design with Unity 2021’ will walk you through creating a multimedia game from scratch, covering everything from the basics of game development to advanced design concepts. The book will help you to learn the ins and outs of scenes, game objects, input systems, physics, particles, and post-processing effects, and even get access to instructions to put your newfound skills to use. In addition, this book will help you to learn the fundamentals of game logic design, interactive narratives, game mechanics, storyboarding, and design structure in an easy-to-understand format from a coaching game expert. 12 | 13 | Whether you're brand new to the gaming industry or a seasoned developer looking to strengthen your Unity skills, this book will provide everything you need to know to design stunning 3D games, animations, 3D content, and virtual reality/augmented reality experiences in Unity 3D. 14 | 15 | ## What You Will Learn 16 | • Use the Unity Game Editor and Assets to design and build 3D animations and games. 17 | 18 | • Understand important game design concepts across a variety of genres. 19 | 20 | • Take advantage of Unity's pre-built UI, rendering, physics, and graphics systems. 21 | 22 | • Create custom gameplay systems and elements using C# scripting. 23 | 24 | • Figure out how to make an already existing prototype appear even better. 25 | -------------------------------------------------------------------------------- /Chapter03/GameSessionManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GameSessionManager : MonoBehaviour 6 | { 7 | 8 | [Tooltip("Remaining player lives.")] 9 | private int _playerLives = 3; 10 | 11 | [SerializeField, Tooltip("Where the player will re-spawn.")] 12 | private Transform _respawnPostion; 13 | 14 | static public GameSessionManager Instance; 15 | 16 | //string levelMap01; 17 | 18 | void Awake() 19 | { 20 | // the GameSessionManager is a Singleton 21 | // store this as the instance of this object 22 | Instance = this; 23 | } 24 | 25 | // Start is called before the first frame update 26 | void Start() 27 | { 28 | } 29 | 30 | // Update is called once per frame 31 | void Update() 32 | { 33 | 34 | } 35 | 36 | public void onPlayerDeath( GameObject player ) 37 | { 38 | public void onPlayerDeath(GameObject player) 39 | { 40 | if (_playerLives <= 0) 41 | { 42 | // player is out of lives 43 | GameObject.Destroy(player.gameObject); 44 | Debug.Log("Game over!"); 45 | } 46 | else 47 | { 48 | // use a life to respawn the player 49 | _playerLives--; 50 | 51 | // reset health 52 | HealthManager playerHealth = player.GetComponent(); 53 | if (playerHealth) 54 | playerHealth.Reset(); 55 | 56 | if (_respawnPostion) 57 | player.transform.position = _respawnPostion.position; 58 | 59 | Debug.Log("Player lives remaining: " + _playerLives); 60 | } 61 | } 62 | 63 | } 64 | 65 | public int GetCoins() { return PickUpItem.s_objectsCollected; } 66 | 67 | public int GetLives() { return _playerLives; } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /Chapter03/HealthModifier.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class HealthModifier : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Change to health when applied to an object.")] 9 | float _healthChange = 0; 10 | 11 | [SerializeField, Tooltip("The class of object that should be damaged.")] 12 | DamageTarget _applyToTarget = DamageTarget.Player; 13 | 14 | public enum DamageTarget 15 | { 16 | Player, 17 | Enemies, 18 | All, 19 | None 20 | } 21 | 22 | [SerializeField, Tooltip("Should this object self-destruct on collision?")] 23 | bool _destroyOnCollision = false; 24 | 25 | // Start is called before the first frame update 26 | void Start() 27 | { 28 | 29 | } 30 | 31 | // Update is called once per frame 32 | void Update() 33 | { 34 | 35 | } 36 | 37 | void OnTriggerStay(Collider collider) 38 | { 39 | GameObject hitObj = collider.gameObject; 40 | 41 | // get the HealthManager of the object we've hit 42 | HealthManager healthManager = hitObj.GetComponent(); 43 | if (healthManager && IsValidTarget(hitObj)) 44 | { 45 | // apply the damage as negative health to this object 46 | healthManager.AdjustCurHealth(_healthChange); 47 | 48 | // should we self-destruct after dealing damage? 49 | if (_destroyOnCollision) 50 | GameObject.Destroy(gameObject); 51 | } 52 | } 53 | 54 | bool IsValidTarget(GameObject possibleTarget) 55 | { 56 | if (_applyToTarget == DamageTarget.All) 57 | return true; 58 | 59 | else if (_applyToTarget == DamageTarget.None) 60 | return false; 61 | 62 | else if (_applyToTarget == DamageTarget.Player && 63 | possibleTarget.GetComponent()) 64 | return true; 65 | 66 | else if (_applyToTarget == DamageTarget.Enemies && 67 | possibleTarget.GetComponent()) 68 | return true; 69 | 70 | // not a valid target 71 | return false; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Chapter04/HealthModifier.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class HealthModifier : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Change to health when applied to an object.")] 9 | float _healthChange = 0; 10 | 11 | [SerializeField, Tooltip("The class of object that should be damaged.")] 12 | DamageTarget _applyToTarget = DamageTarget.Player; 13 | 14 | public enum DamageTarget 15 | { 16 | Player, 17 | Enemies, 18 | All, 19 | None 20 | } 21 | 22 | [SerializeField, Tooltip("Should this object self-destruct on collision?")] 23 | bool _destroyOnCollision = false; 24 | 25 | // Start is called before the first frame update 26 | void Start() 27 | { 28 | 29 | } 30 | 31 | // Update is called once per frame 32 | void Update() 33 | { 34 | 35 | } 36 | 37 | void OnTriggerStay(Collider collider) 38 | { 39 | GameObject hitObj = collider.gameObject; 40 | 41 | // get the HealthManager of the object we've hit 42 | HealthManager healthManager = hitObj.GetComponent(); 43 | if (healthManager && IsValidTarget(hitObj)) 44 | { 45 | // apply the damage as negative health to this object 46 | healthManager.AdjustCurHealth(_healthChange); 47 | 48 | // should we self-destruct after dealing damage? 49 | if (_destroyOnCollision) 50 | GameObject.Destroy(gameObject); 51 | } 52 | } 53 | 54 | bool IsValidTarget(GameObject possibleTarget) 55 | { 56 | if (_applyToTarget == DamageTarget.All) 57 | return true; 58 | 59 | else if (_applyToTarget == DamageTarget.None) 60 | return false; 61 | 62 | else if (_applyToTarget == DamageTarget.Player && 63 | possibleTarget.GetComponent()) 64 | return true; 65 | 66 | else if (_applyToTarget == DamageTarget.Enemies && 67 | possibleTarget.GetComponent()) 68 | return true; 69 | 70 | // not a valid target 71 | return false; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Chapter05/HealthModifier.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class HealthModifier : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Change to health when applied to an object.")] 9 | float _healthChange = 0; 10 | 11 | [SerializeField, Tooltip("The class of object that should be damaged.")] 12 | DamageTarget _applyToTarget = DamageTarget.Player; 13 | 14 | public enum DamageTarget 15 | { 16 | Player, 17 | Enemies, 18 | All, 19 | None 20 | } 21 | 22 | [SerializeField, Tooltip("Should this object self-destruct on collision?")] 23 | bool _destroyOnCollision = false; 24 | 25 | // Start is called before the first frame update 26 | void Start() 27 | { 28 | 29 | } 30 | 31 | // Update is called once per frame 32 | void Update() 33 | { 34 | 35 | } 36 | 37 | void OnTriggerStay(Collider collider) 38 | { 39 | GameObject hitObj = collider.gameObject; 40 | 41 | // get the HealthManager of the object we've hit 42 | HealthManager healthManager = hitObj.GetComponent(); 43 | if (healthManager && IsValidTarget(hitObj)) 44 | { 45 | // apply the damage as negative health to this object 46 | healthManager.AdjustCurHealth(_healthChange); 47 | 48 | // should we self-destruct after dealing damage? 49 | if (_destroyOnCollision) 50 | GameObject.Destroy(gameObject); 51 | } 52 | } 53 | 54 | bool IsValidTarget(GameObject possibleTarget) 55 | { 56 | if (_applyToTarget == DamageTarget.All) 57 | return true; 58 | 59 | else if (_applyToTarget == DamageTarget.None) 60 | return false; 61 | 62 | else if (_applyToTarget == DamageTarget.Player && 63 | possibleTarget.GetComponent()) 64 | return true; 65 | 66 | else if (_applyToTarget == DamageTarget.Enemies && 67 | possibleTarget.GetComponent()) 68 | return true; 69 | 70 | // not a valid target 71 | return false; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Chapter06/HealthModifier.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class HealthModifier : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Change to health when applied to an object.")] 9 | float _healthChange = 0; 10 | 11 | [SerializeField, Tooltip("The class of object that should be damaged.")] 12 | DamageTarget _applyToTarget = DamageTarget.Player; 13 | 14 | public enum DamageTarget 15 | { 16 | Player, 17 | Enemies, 18 | All, 19 | None 20 | } 21 | 22 | [SerializeField, Tooltip("Should this object self-destruct on collision?")] 23 | bool _destroyOnCollision = false; 24 | 25 | // Start is called before the first frame update 26 | void Start() 27 | { 28 | 29 | } 30 | 31 | // Update is called once per frame 32 | void Update() 33 | { 34 | 35 | } 36 | 37 | void OnTriggerStay(Collider collider) 38 | { 39 | GameObject hitObj = collider.gameObject; 40 | 41 | // get the HealthManager of the object we've hit 42 | HealthManager healthManager = hitObj.GetComponent(); 43 | if (healthManager && IsValidTarget(hitObj)) 44 | { 45 | // apply the damage as negative health to this object 46 | healthManager.AdjustCurHealth(_healthChange); 47 | 48 | // should we self-destruct after dealing damage? 49 | if (_destroyOnCollision) 50 | GameObject.Destroy(gameObject); 51 | } 52 | } 53 | 54 | bool IsValidTarget(GameObject possibleTarget) 55 | { 56 | if (_applyToTarget == DamageTarget.All) 57 | return true; 58 | 59 | else if (_applyToTarget == DamageTarget.None) 60 | return false; 61 | 62 | else if (_applyToTarget == DamageTarget.Player && 63 | possibleTarget.GetComponent()) 64 | return true; 65 | 66 | else if (_applyToTarget == DamageTarget.Enemies && 67 | possibleTarget.GetComponent()) 68 | return true; 69 | 70 | // not a valid target 71 | return false; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Chapter07/HealthModifier.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class HealthModifier : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Change to health when applied to an object.")] 9 | float _healthChange = 0; 10 | 11 | [SerializeField, Tooltip("The class of object that should be damaged.")] 12 | DamageTarget _applyToTarget = DamageTarget.Player; 13 | 14 | public enum DamageTarget 15 | { 16 | Player, 17 | Enemies, 18 | All, 19 | None 20 | } 21 | 22 | [SerializeField, Tooltip("Should this object self-destruct on collision?")] 23 | bool _destroyOnCollision = false; 24 | 25 | // Start is called before the first frame update 26 | void Start() 27 | { 28 | 29 | } 30 | 31 | // Update is called once per frame 32 | void Update() 33 | { 34 | 35 | } 36 | 37 | void OnTriggerStay(Collider collider) 38 | { 39 | GameObject hitObj = collider.gameObject; 40 | 41 | // get the HealthManager of the object we've hit 42 | HealthManager healthManager = hitObj.GetComponent(); 43 | if (healthManager && IsValidTarget(hitObj)) 44 | { 45 | // apply the damage as negative health to this object 46 | healthManager.AdjustCurHealth(_healthChange); 47 | 48 | // should we self-destruct after dealing damage? 49 | if (_destroyOnCollision) 50 | GameObject.Destroy(gameObject); 51 | } 52 | } 53 | 54 | bool IsValidTarget(GameObject possibleTarget) 55 | { 56 | if (_applyToTarget == DamageTarget.All) 57 | return true; 58 | 59 | else if (_applyToTarget == DamageTarget.None) 60 | return false; 61 | 62 | else if (_applyToTarget == DamageTarget.Player && 63 | possibleTarget.GetComponent()) 64 | return true; 65 | 66 | else if (_applyToTarget == DamageTarget.Enemies && 67 | possibleTarget.GetComponent()) 68 | return true; 69 | 70 | // not a valid target 71 | return false; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Chapter08/HealthModifier.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class HealthModifier : MonoBehaviour 6 | { 7 | 8 | [SerializeField, Tooltip("Change to health when applied to an object.")] 9 | float _healthChange = 0; 10 | 11 | [SerializeField, Tooltip("The class of object that should be damaged.")] 12 | DamageTarget _applyToTarget = DamageTarget.Player; 13 | 14 | public enum DamageTarget 15 | { 16 | Player, 17 | Enemies, 18 | All, 19 | None 20 | } 21 | 22 | [SerializeField, Tooltip("Should this object self-destruct on collision?")] 23 | bool _destroyOnCollision = false; 24 | 25 | // Start is called before the first frame update 26 | void Start() 27 | { 28 | 29 | } 30 | 31 | // Update is called once per frame 32 | void Update() 33 | { 34 | 35 | } 36 | 37 | void OnTriggerStay(Collider collider) 38 | { 39 | GameObject hitObj = collider.gameObject; 40 | 41 | // get the HealthManager of the object we've hit 42 | HealthManager healthManager = hitObj.GetComponent(); 43 | if (healthManager && IsValidTarget(hitObj)) 44 | { 45 | // apply the damage as negative health to this object 46 | healthManager.AdjustCurHealth(_healthChange); 47 | 48 | // should we self-destruct after dealing damage? 49 | if (_destroyOnCollision) 50 | GameObject.Destroy(gameObject); 51 | } 52 | } 53 | 54 | bool IsValidTarget(GameObject possibleTarget) 55 | { 56 | if (_applyToTarget == DamageTarget.All) 57 | return true; 58 | 59 | else if (_applyToTarget == DamageTarget.None) 60 | return false; 61 | 62 | else if (_applyToTarget == DamageTarget.Player && 63 | possibleTarget.GetComponent()) 64 | return true; 65 | 66 | else if (_applyToTarget == DamageTarget.Enemies && 67 | possibleTarget.GetComponent()) 68 | return true; 69 | 70 | // not a valid target 71 | return false; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Chapter04/GameSessionManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GameSessionManager : MonoBehaviour 6 | { 7 | 8 | [Tooltip("Remaining player lives.")] 9 | private int _playerLives = 3; 10 | 11 | [SerializeField, Tooltip("Where the player will re-spawn.")] 12 | private Transform _respawnPostion; 13 | 14 | [SerializeField, Tooltip("Object to display when the game is over.")] 15 | private GameObject _gameOverObj; 16 | 17 | [SerializeField, Tooltip("Title Menu countdown after the game is over.")] 18 | private float _returnToMenuCountdown = 0; 19 | 20 | static public GameSessionManager Instance; 21 | 22 | void Awake() 23 | { 24 | // the GameSessionManager is a Singleton 25 | // store this as the instance of this object 26 | Instance = this; 27 | } 28 | 29 | // Start is called before the first frame update 30 | void Start() 31 | { 32 | } 33 | 34 | // Update is called once per frame 35 | void Update() 36 | { 37 | if (_returnToMenuCountdown > 0) 38 | { 39 | _returnToMenuCountdown -= Time.deltaTime; 40 | if (_returnToMenuCountdown < 0) 41 | SceneManager.LoadScene("TitleMenu"); 42 | } 43 | 44 | } 45 | 46 | public void onPlayerDeath( GameObject player ) 47 | { 48 | public void onPlayerDeath(GameObject player) 49 | { 50 | if (_playerLives <= 0) 51 | { 52 | // player is out of lives 53 | GameObject.Destroy(player.gameObject); 54 | Debug.Log("Game over!"); 55 | 56 | _gameOverObj.SetActive(true); 57 | _returnToMenuCountdown = 4; 58 | 59 | } 60 | else 61 | { 62 | // use a life to respawn the player 63 | _playerLives--; 64 | 65 | // reset health 66 | HealthManager playerHealth = player.GetComponent(); 67 | if (playerHealth) 68 | playerHealth.Reset(); 69 | 70 | if (_respawnPostion) 71 | player.transform.position = _respawnPostion.position; 72 | 73 | Debug.Log("Player lives remaining: " + _playerLives); 74 | } 75 | } 76 | 77 | } 78 | 79 | public int GetCoins() { return PickUpItem.s_objectsCollected; } 80 | 81 | public int GetLives() { return _playerLives; } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /Chapter13/TorchfireBlock.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class TorchfireBlock : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Seconds until fire goes out.")] 8 | float _fireTimer = 0; 9 | 10 | [SerializeField, Tooltip("Seconds applied when re-lit.")] 11 | float _maxTimer = 15; 12 | 13 | [SerializeField, Tooltip("The fire particle effect.")] 14 | ParticleSystem _fireParticle; 15 | 16 | // number of blocks in the level that are currently lit 17 | static int s_numLitBlocks = 0; 18 | 19 | // Start is called before the first frame update 20 | void Start() 21 | { 22 | EnableFireEffects(false); 23 | EnableSceneLights(false); 24 | } 25 | 26 | // Update is called once per frame 27 | void Update() 28 | { 29 | if (_fireTimer > 0f) 30 | { 31 | _fireTimer -= Time.deltaTime; 32 | if (_fireTimer < 0) 33 | { 34 | // fire has gone out 35 | AdjustFireBlockCount(-1); 36 | EnableFireEffects(false); 37 | } 38 | } 39 | } 40 | 41 | void EnableFireEffects(bool status) 42 | { 43 | // grab the particle effect emitter 44 | // and enable (or disable) it 45 | var emit = _fireParticle.emission; 46 | emit.enabled = status; 47 | } 48 | 49 | void OnTriggerEnter(Collider collider) 50 | { 51 | // did we collide with a flamable object? 52 | if (collider.gameObject.GetComponent()) 53 | { 54 | // re-light the pyre 55 | if (_fireTimer <= 0) 56 | { 57 | AdjustFireBlockCount(1); 58 | EnableFireEffects(true); 59 | } 60 | 61 | _fireTimer = _maxTimer; 62 | } 63 | } 64 | 65 | static public void AdjustFireBlockCount(int change) 66 | { 67 | // update number of 'lit' torchfire 68 | // blocks in the scene 69 | s_numLitBlocks += change; 70 | 71 | // enable / disable the lights based on 72 | // the number of lit blocks 73 | bool lightStatus = true; 74 | if (s_numLitBlocks <= 0) 75 | lightStatus = false; 76 | 77 | EnableSceneLights(lightStatus); 78 | } 79 | 80 | static public void EnableSceneLights(bool status) 81 | { 82 | Light[] lights = FindObjectsOfType(); 83 | foreach (Light l in lights) 84 | { 85 | if (l.type == LightType.Directional) 86 | l.enabled = status; 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /Chapter14/TorchfireBlock.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class TorchfireBlock : MonoBehaviour 6 | { 7 | [SerializeField, Tooltip("Seconds until fire goes out.")] 8 | float _fireTimer = 0; 9 | 10 | [SerializeField, Tooltip("Seconds applied when re-lit.")] 11 | float _maxTimer = 15; 12 | 13 | [SerializeField, Tooltip("The fire particle effect.")] 14 | ParticleSystem _fireParticle; 15 | 16 | // number of blocks in the level that are currently lit 17 | static int s_numLitBlocks = 0; 18 | 19 | // Start is called before the first frame update 20 | void Start() 21 | { 22 | EnableFireEffects(false); 23 | EnableSceneLights(false); 24 | } 25 | 26 | // Update is called once per frame 27 | void Update() 28 | { 29 | if (_fireTimer > 0f) 30 | { 31 | _fireTimer -= Time.deltaTime; 32 | if (_fireTimer < 0) 33 | { 34 | // fire has gone out 35 | AdjustFireBlockCount(-1); 36 | EnableFireEffects(false); 37 | } 38 | } 39 | } 40 | 41 | void EnableFireEffects(bool status) 42 | { 43 | // grab the particle effect emitter 44 | // and enable (or disable) it 45 | var emit = _fireParticle.emission; 46 | emit.enabled = status; 47 | } 48 | 49 | void OnTriggerEnter(Collider collider) 50 | { 51 | // did we collide with a flamable object? 52 | if (collider.gameObject.GetComponent()) 53 | { 54 | // re-light the pyre 55 | if (_fireTimer <= 0) 56 | { 57 | AdjustFireBlockCount(1); 58 | EnableFireEffects(true); 59 | } 60 | 61 | _fireTimer = _maxTimer; 62 | } 63 | } 64 | 65 | static public void AdjustFireBlockCount(int change) 66 | { 67 | // update number of 'lit' torchfire 68 | // blocks in the scene 69 | s_numLitBlocks += change; 70 | 71 | // enable / disable the lights based on 72 | // the number of lit blocks 73 | bool lightStatus = true; 74 | if (s_numLitBlocks <= 0) 75 | lightStatus = false; 76 | 77 | EnableSceneLights(lightStatus); 78 | } 79 | 80 | static public void EnableSceneLights(bool status) 81 | { 82 | Light[] lights = FindObjectsOfType(); 83 | foreach (Light l in lights) 84 | { 85 | if (l.type == LightType.Directional) 86 | l.enabled = status; 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /Chapter05/GameSessionManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GameSessionManager : MonoBehaviour 6 | { 7 | 8 | [Tooltip("Remaining player lives.")] 9 | private int _playerLives = 3; 10 | 11 | [SerializeField, Tooltip("Where the player will re-spawn.")] 12 | private Transform _respawnPostion; 13 | 14 | [SerializeField, Tooltip("Object to display when the game is over.")] 15 | private GameObject _gameOverObj; 16 | 17 | [SerializeField, Tooltip("Title Menu countdown after the game is over.")] 18 | private float _returnToMenuCountdown = 0; 19 | 20 | static public GameSessionManager Instance; 21 | 22 | string levelMap01; 23 | 24 | void Awake() 25 | { 26 | // the GameSessionManager is a Singleton 27 | // store this as the instance of this object 28 | Instance = this; 29 | } 30 | 31 | // Start is called before the first frame update 32 | void Start() 33 | { 34 | } 35 | 36 | // Update is called once per frame 37 | void Update() 38 | { 39 | if (_returnToMenuCountdown > 0) 40 | { 41 | _returnToMenuCountdown -= Time.deltaTime; 42 | if (_returnToMenuCountdown < 0) 43 | UnityEngine.SceneManagement.SceneManager.LoadScene("TitleMenu"); 44 | } 45 | } 46 | 47 | public void onPlayerDeath( GameObject player ) 48 | { 49 | if (_playerLives <= 0) 50 | { 51 | // player is out of lives 52 | GameObject.Destroy(player.gameObject); 53 | 54 | _gameOverObj.SetActive(true); 55 | _returnToMenuCountdown = 4; 56 | 57 | Debug.Log("Game over!"); 58 | 59 | //SceneManager.LoadScene("SampleScene"); 60 | } 61 | else 62 | { 63 | // use a life to respawn the player 64 | _playerLives--; 65 | 66 | // reset health 67 | HealthManager playerHealth = player.GetComponent(); 68 | if (playerHealth) 69 | playerHealth.Reset(); 70 | 71 | if (_respawnPostion) 72 | player.transform.position = _respawnPostion.position; 73 | 74 | // clear the velocity of this object 75 | Rigidbody rb = player.transform.GetComponent(); 76 | if (rb) 77 | rb.velocity = Vector3.zero; 78 | 79 | Debug.Log("Player lives remaining: " + _playerLives); 80 | } 81 | } 82 | 83 | public int GetCoins() { return PickUpItem.s_objectsCollected; } 84 | 85 | public int GetLives() { return _playerLives; } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /Chapter06/GameSessionManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GameSessionManager : MonoBehaviour 6 | { 7 | 8 | [Tooltip("Remaining player lives.")] 9 | private int _playerLives = 3; 10 | 11 | [SerializeField, Tooltip("Where the player will re-spawn.")] 12 | private Transform _respawnPostion; 13 | 14 | [SerializeField, Tooltip("Object to display when the game is over.")] 15 | private GameObject _gameOverObj; 16 | 17 | [SerializeField, Tooltip("Title Menu countdown after the game is over.")] 18 | private float _returnToMenuCountdown = 0; 19 | 20 | static public GameSessionManager Instance; 21 | 22 | string levelMap01; 23 | 24 | void Awake() 25 | { 26 | // the GameSessionManager is a Singleton 27 | // store this as the instance of this object 28 | Instance = this; 29 | } 30 | 31 | // Start is called before the first frame update 32 | void Start() 33 | { 34 | } 35 | 36 | // Update is called once per frame 37 | void Update() 38 | { 39 | if (_returnToMenuCountdown > 0) 40 | { 41 | _returnToMenuCountdown -= Time.deltaTime; 42 | if (_returnToMenuCountdown < 0) 43 | UnityEngine.SceneManagement.SceneManager.LoadScene("TitleMenu"); 44 | } 45 | } 46 | 47 | public void onPlayerDeath( GameObject player ) 48 | { 49 | if (_playerLives <= 0) 50 | { 51 | // player is out of lives 52 | GameObject.Destroy(player.gameObject); 53 | 54 | _gameOverObj.SetActive(true); 55 | _returnToMenuCountdown = 4; 56 | 57 | Debug.Log("Game over!"); 58 | 59 | //SceneManager.LoadScene("SampleScene"); 60 | } 61 | else 62 | { 63 | // use a life to respawn the player 64 | _playerLives--; 65 | 66 | // reset health 67 | HealthManager playerHealth = player.GetComponent(); 68 | if (playerHealth) 69 | playerHealth.Reset(); 70 | 71 | if (_respawnPostion) 72 | player.transform.position = _respawnPostion.position; 73 | 74 | // clear the velocity of this object 75 | Rigidbody rb = player.transform.GetComponent(); 76 | if (rb) 77 | rb.velocity = Vector3.zero; 78 | 79 | Debug.Log("Player lives remaining: " + _playerLives); 80 | } 81 | } 82 | 83 | public int GetCoins() { return PickUpItem.s_objectsCollected; } 84 | 85 | public int GetLives() { return _playerLives; } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /Chapter07/GameSessionManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GameSessionManager : MonoBehaviour 6 | { 7 | 8 | [Tooltip("Remaining player lives.")] 9 | private int _playerLives = 3; 10 | 11 | [SerializeField, Tooltip("Where the player will re-spawn.")] 12 | private Transform _respawnPostion; 13 | 14 | [SerializeField, Tooltip("Object to display when the game is over.")] 15 | private GameObject _gameOverObj; 16 | 17 | [SerializeField, Tooltip("Title Menu countdown after the game is over.")] 18 | private float _returnToMenuCountdown = 0; 19 | 20 | static public GameSessionManager Instance; 21 | 22 | string levelMap01; 23 | 24 | void Awake() 25 | { 26 | // the GameSessionManager is a Singleton 27 | // store this as the instance of this object 28 | Instance = this; 29 | } 30 | 31 | // Start is called before the first frame update 32 | void Start() 33 | { 34 | } 35 | 36 | // Update is called once per frame 37 | void Update() 38 | { 39 | if (_returnToMenuCountdown > 0) 40 | { 41 | _returnToMenuCountdown -= Time.deltaTime; 42 | if (_returnToMenuCountdown < 0) 43 | UnityEngine.SceneManagement.SceneManager.LoadScene("TitleMenu"); 44 | } 45 | } 46 | 47 | public void onPlayerDeath( GameObject player ) 48 | { 49 | if (_playerLives <= 0) 50 | { 51 | // player is out of lives 52 | GameObject.Destroy(player.gameObject); 53 | 54 | _gameOverObj.SetActive(true); 55 | _returnToMenuCountdown = 4; 56 | 57 | Debug.Log("Game over!"); 58 | 59 | //SceneManager.LoadScene("SampleScene"); 60 | } 61 | else 62 | { 63 | // use a life to respawn the player 64 | _playerLives--; 65 | 66 | // reset health 67 | HealthManager playerHealth = player.GetComponent(); 68 | if (playerHealth) 69 | playerHealth.Reset(); 70 | 71 | if (_respawnPostion) 72 | player.transform.position = _respawnPostion.position; 73 | 74 | // clear the velocity of this object 75 | Rigidbody rb = player.transform.GetComponent(); 76 | if (rb) 77 | rb.velocity = Vector3.zero; 78 | 79 | Debug.Log("Player lives remaining: " + _playerLives); 80 | } 81 | } 82 | 83 | public int GetCoins() { return PickUpItem.s_objectsCollected; } 84 | 85 | public int GetLives() { return _playerLives; } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /Chapter08/GameSessionManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GameSessionManager : MonoBehaviour 6 | { 7 | 8 | [Tooltip("Remaining player lives.")] 9 | private int _playerLives = 3; 10 | 11 | [SerializeField, Tooltip("Where the player will re-spawn.")] 12 | private Transform _respawnPostion; 13 | 14 | [SerializeField, Tooltip("Object to display when the game is over.")] 15 | private GameObject _gameOverObj; 16 | 17 | [SerializeField, Tooltip("Title Menu countdown after the game is over.")] 18 | private float _returnToMenuCountdown = 0; 19 | 20 | static public GameSessionManager Instance; 21 | 22 | string levelMap01; 23 | 24 | void Awake() 25 | { 26 | // the GameSessionManager is a Singleton 27 | // store this as the instance of this object 28 | Instance = this; 29 | } 30 | 31 | // Start is called before the first frame update 32 | void Start() 33 | { 34 | } 35 | 36 | // Update is called once per frame 37 | void Update() 38 | { 39 | if (_returnToMenuCountdown > 0) 40 | { 41 | _returnToMenuCountdown -= Time.deltaTime; 42 | if (_returnToMenuCountdown < 0) 43 | UnityEngine.SceneManagement.SceneManager.LoadScene("TitleMenu"); 44 | } 45 | } 46 | 47 | public void onPlayerDeath( GameObject player ) 48 | { 49 | if (_playerLives <= 0) 50 | { 51 | // player is out of lives 52 | GameObject.Destroy(player.gameObject); 53 | 54 | _gameOverObj.SetActive(true); 55 | _returnToMenuCountdown = 4; 56 | 57 | Debug.Log("Game over!"); 58 | 59 | //SceneManager.LoadScene("SampleScene"); 60 | } 61 | else 62 | { 63 | // use a life to respawn the player 64 | _playerLives--; 65 | 66 | // reset health 67 | HealthManager playerHealth = player.GetComponent(); 68 | if (playerHealth) 69 | playerHealth.Reset(); 70 | 71 | if (_respawnPostion) 72 | player.transform.position = _respawnPostion.position; 73 | 74 | // clear the velocity of this object 75 | Rigidbody rb = player.transform.GetComponent(); 76 | if (rb) 77 | rb.velocity = Vector3.zero; 78 | 79 | Debug.Log("Player lives remaining: " + _playerLives); 80 | } 81 | } 82 | 83 | public int GetCoins() { return PickUpItem.s_objectsCollected; } 84 | 85 | public int GetLives() { return _playerLives; } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /Chapter09/GameSessionManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GameSessionManager : MonoBehaviour 6 | { 7 | 8 | [Tooltip("Remaining player lives.")] 9 | private int _playerLives = 3; 10 | 11 | [SerializeField, Tooltip("Where the player will re-spawn.")] 12 | private Transform _respawnPostion; 13 | 14 | [SerializeField, Tooltip("Object to display when the game is over.")] 15 | private GameObject _gameOverObj; 16 | 17 | [SerializeField, Tooltip("Title Menu countdown after the game is over.")] 18 | private float _returnToMenuCountdown = 0; 19 | 20 | static public GameSessionManager Instance; 21 | 22 | string levelMap01; 23 | 24 | void Awake() 25 | { 26 | // the GameSessionManager is a Singleton 27 | // store this as the instance of this object 28 | Instance = this; 29 | } 30 | 31 | // Start is called before the first frame update 32 | void Start() 33 | { 34 | } 35 | 36 | // Update is called once per frame 37 | void Update() 38 | { 39 | if (_returnToMenuCountdown > 0) 40 | { 41 | _returnToMenuCountdown -= Time.deltaTime; 42 | if (_returnToMenuCountdown < 0) 43 | UnityEngine.SceneManagement.SceneManager.LoadScene("TitleMenu"); 44 | } 45 | } 46 | 47 | public void onPlayerDeath( GameObject player ) 48 | { 49 | if (_playerLives <= 0) 50 | { 51 | // player is out of lives 52 | GameObject.Destroy(player.gameObject); 53 | 54 | _gameOverObj.SetActive(true); 55 | _returnToMenuCountdown = 4; 56 | 57 | Debug.Log("Game over!"); 58 | 59 | //SceneManager.LoadScene("SampleScene"); 60 | } 61 | else 62 | { 63 | // use a life to respawn the player 64 | _playerLives--; 65 | 66 | // reset health 67 | HealthManager playerHealth = player.GetComponent(); 68 | if (playerHealth) 69 | playerHealth.Reset(); 70 | 71 | if (_respawnPostion) 72 | player.transform.position = _respawnPostion.position; 73 | 74 | // clear the velocity of this object 75 | Rigidbody rb = player.transform.GetComponent(); 76 | if (rb) 77 | rb.velocity = Vector3.zero; 78 | 79 | Debug.Log("Player lives remaining: " + _playerLives); 80 | } 81 | } 82 | 83 | public int GetCoins() { return PickUpItem.s_objectsCollected; } 84 | 85 | public int GetLives() { return _playerLives; } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /Chapter10/GameSessionManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GameSessionManager : MonoBehaviour 6 | { 7 | 8 | [Tooltip("Remaining player lives.")] 9 | private int _playerLives = 3; 10 | 11 | [SerializeField, Tooltip("Where the player will re-spawn.")] 12 | private Transform _respawnPostion; 13 | 14 | [SerializeField, Tooltip("Object to display when the game is over.")] 15 | private GameObject _gameOverObj; 16 | 17 | [SerializeField, Tooltip("Title Menu countdown after the game is over.")] 18 | private float _returnToMenuCountdown = 0; 19 | 20 | static public GameSessionManager Instance; 21 | 22 | string levelMap01; 23 | 24 | void Awake() 25 | { 26 | // the GameSessionManager is a Singleton 27 | // store this as the instance of this object 28 | Instance = this; 29 | } 30 | 31 | // Start is called before the first frame update 32 | void Start() 33 | { 34 | } 35 | 36 | // Update is called once per frame 37 | void Update() 38 | { 39 | if (_returnToMenuCountdown > 0) 40 | { 41 | _returnToMenuCountdown -= Time.deltaTime; 42 | if (_returnToMenuCountdown < 0) 43 | UnityEngine.SceneManagement.SceneManager.LoadScene("TitleMenu"); 44 | } 45 | } 46 | 47 | public void onPlayerDeath( GameObject player ) 48 | { 49 | if (_playerLives <= 0) 50 | { 51 | // player is out of lives 52 | GameObject.Destroy(player.gameObject); 53 | 54 | _gameOverObj.SetActive(true); 55 | _returnToMenuCountdown = 4; 56 | 57 | Debug.Log("Game over!"); 58 | 59 | //SceneManager.LoadScene("SampleScene"); 60 | } 61 | else 62 | { 63 | // use a life to respawn the player 64 | _playerLives--; 65 | 66 | // reset health 67 | HealthManager playerHealth = player.GetComponent(); 68 | if (playerHealth) 69 | playerHealth.Reset(); 70 | 71 | if (_respawnPostion) 72 | player.transform.position = _respawnPostion.position; 73 | 74 | // clear the velocity of this object 75 | Rigidbody rb = player.transform.GetComponent(); 76 | if (rb) 77 | rb.velocity = Vector3.zero; 78 | 79 | Debug.Log("Player lives remaining: " + _playerLives); 80 | } 81 | } 82 | 83 | public int GetCoins() { return PickUpItem.s_objectsCollected; } 84 | 85 | public int GetLives() { return _playerLives; } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /Chapter11/GameSessionManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GameSessionManager : MonoBehaviour 6 | { 7 | 8 | [Tooltip("Remaining player lives.")] 9 | private int _playerLives = 3; 10 | 11 | [SerializeField, Tooltip("Where the player will re-spawn.")] 12 | private Transform _respawnPostion; 13 | 14 | [SerializeField, Tooltip("Object to display when the game is over.")] 15 | private GameObject _gameOverObj; 16 | 17 | [SerializeField, Tooltip("Title Menu countdown after the game is over.")] 18 | private float _returnToMenuCountdown = 0; 19 | 20 | static public GameSessionManager Instance; 21 | 22 | string levelMap01; 23 | 24 | void Awake() 25 | { 26 | // the GameSessionManager is a Singleton 27 | // store this as the instance of this object 28 | Instance = this; 29 | } 30 | 31 | // Start is called before the first frame update 32 | void Start() 33 | { 34 | } 35 | 36 | // Update is called once per frame 37 | void Update() 38 | { 39 | if (_returnToMenuCountdown > 0) 40 | { 41 | _returnToMenuCountdown -= Time.deltaTime; 42 | if (_returnToMenuCountdown < 0) 43 | UnityEngine.SceneManagement.SceneManager.LoadScene("TitleMenu"); 44 | } 45 | } 46 | 47 | public void onPlayerDeath( GameObject player ) 48 | { 49 | if (_playerLives <= 0) 50 | { 51 | // player is out of lives 52 | GameObject.Destroy(player.gameObject); 53 | 54 | _gameOverObj.SetActive(true); 55 | _returnToMenuCountdown = 4; 56 | 57 | Debug.Log("Game over!"); 58 | 59 | //SceneManager.LoadScene("SampleScene"); 60 | } 61 | else 62 | { 63 | // use a life to respawn the player 64 | _playerLives--; 65 | 66 | // reset health 67 | HealthManager playerHealth = player.GetComponent(); 68 | if (playerHealth) 69 | playerHealth.Reset(); 70 | 71 | if (_respawnPostion) 72 | player.transform.position = _respawnPostion.position; 73 | 74 | // clear the velocity of this object 75 | Rigidbody rb = player.transform.GetComponent(); 76 | if (rb) 77 | rb.velocity = Vector3.zero; 78 | 79 | Debug.Log("Player lives remaining: " + _playerLives); 80 | } 81 | } 82 | 83 | public int GetCoins() { return PickUpItem.s_objectsCollected; } 84 | 85 | public int GetLives() { return _playerLives; } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /Chapter12/GameSessionManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class GameSessionManager : MonoBehaviour 6 | { 7 | 8 | [Tooltip("Remaining player lives.")] 9 | private int _playerLives = 3; 10 | 11 | [SerializeField, Tooltip("Where the player will re-spawn.")] 12 | private Transform _respawnPostion; 13 | 14 | [SerializeField, Tooltip("Object to display when the game is over.")] 15 | private GameObject _gameOverObj; 16 | 17 | [SerializeField, Tooltip("Title Menu countdown after the game is over.")] 18 | private float _returnToMenuCountdown = 0; 19 | 20 | static public GameSessionManager Instance; 21 | 22 | string levelMap01; 23 | 24 | void Awake() 25 | { 26 | // the GameSessionManager is a Singleton 27 | // store this as the instance of this object 28 | Instance = this; 29 | } 30 | 31 | // Start is called before the first frame update 32 | void Start() 33 | { 34 | } 35 | 36 | // Update is called once per frame 37 | void Update() 38 | { 39 | if (_returnToMenuCountdown > 0) 40 | { 41 | _returnToMenuCountdown -= Time.deltaTime; 42 | if (_returnToMenuCountdown < 0) 43 | UnityEngine.SceneManagement.SceneManager.LoadScene("TitleMenu"); 44 | } 45 | } 46 | 47 | public void onPlayerDeath( GameObject player ) 48 | { 49 | if (_playerLives <= 0) 50 | { 51 | // player is out of lives 52 | GameObject.Destroy(player.gameObject); 53 | 54 | _gameOverObj.SetActive(true); 55 | _returnToMenuCountdown = 4; 56 | 57 | Debug.Log("Game over!"); 58 | 59 | //SceneManager.LoadScene("SampleScene"); 60 | } 61 | else 62 | { 63 | // use a life to respawn the player 64 | _playerLives--; 65 | 66 | // reset health 67 | HealthManager playerHealth = player.GetComponent(); 68 | if (playerHealth) 69 | playerHealth.Reset(); 70 | 71 | if (_respawnPostion) 72 | player.transform.position = _respawnPostion.position; 73 | 74 | // clear the velocity of this object 75 | Rigidbody rb = player.transform.GetComponent(); 76 | if (rb) 77 | rb.velocity = Vector3.zero; 78 | 79 | Debug.Log("Player lives remaining: " + _playerLives); 80 | } 81 | } 82 | 83 | public int GetCoins() { return PickUpItem.s_objectsCollected; } 84 | 85 | public int GetLives() { return _playerLives; } 86 | 87 | } 88 | --------------------------------------------------------------------------------