public IEnumerator DeliverPickup () { if (playerHealth == null) { Debug.LogWarning ("Player Health Not Found! Looking again..."); playerHealth = GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayersHealth> (); } // Wait for the delivery delay. yield return new WaitForSeconds (pickupDeliveryTime); // Create a random x coordinate for the delivery in the drop range. float dropPosX = Random.Range (dropRangeLeft, dropRangeRight); // Create a position with the random x coordinate. Vector3 dropPos = new Vector3 (dropPosX, 15f, 1f); // If the player's health is above the high threshold... if (playerHealth.health >= highHealthThreshold) // ... instantiate a bomb pickup at the drop position. Instantiate (pickups [0], dropPos, Quaternion.identity); // Otherwise if the player's health is below the low threshold... else if (playerHealth.health <= lowHealthThreshold) // ... instantiate a health pickup at the drop position. Instantiate (pickups [1], dropPos, Quaternion.identity); // Otherwise... else { // ... instantiate a random pickup at the drop position. int pickupIndex = Random.Range (0, pickups.Length); Instantiate (pickups [pickupIndex], dropPos, Quaternion.identity); } }
void OnTriggerEnter2D(Collider2D other) { // If the player enters the trigger zone... if (other.tag == "Player") { // Get a reference to the player health script. PlayersHealth playerHealth = other.GetComponent <PlayersHealth> (); // Increasse the player's health by the health bonus but clamp it at 100. playerHealth.health += healthBonus; playerHealth.health = Mathf.Clamp(playerHealth.health, 0f, 100f); // Update the health bar. playerHealth.UpdateHealthBar(); // Trigger a new delivery. pickupSpawner.StartCoroutine(pickupSpawner.DeliverPickup()); // Play the collection sound. AudioSource.PlayClipAtPoint(collect, transform.position); // Destroy the crate. Destroy(transform.root.gameObject); } // Otherwise if the crate hits the ground... else if (other.tag == "ground" && !landed) { // ... set the Land animator trigger parameter. anim.SetTrigger("Land"); transform.parent = null; gameObject.AddComponent <Rigidbody2D> (); landed = true; } }
private PlayersHealth playerHealth; // Reference to the PlayerHealth script. void Awake () { GameObject player = GameObject.FindGameObjectWithTag ("Player"); //Debug.Log ("Found " + player.name); playerHealth = player.GetComponent<PlayersHealth> (); if (playerHealth == null) Debug.LogWarning ("Player Health Not Found!"); }
private PlayersHealth playerHealth; // Reference to the PlayerHealth script. void Awake() { GameObject player = GameObject.FindGameObjectWithTag("Player"); //Debug.Log ("Found " + player.name); playerHealth = player.GetComponent <PlayersHealth> (); if (playerHealth == null) { Debug.LogWarning("Player Health Not Found!"); } }
public IEnumerator DeliverPickup() { if (playerHealth == null) { Debug.LogWarning("Player Health Not Found! Looking again..."); playerHealth = GameObject.FindGameObjectWithTag("Player").GetComponent <PlayersHealth> (); } // Wait for the delivery delay. yield return(new WaitForSeconds(pickupDeliveryTime)); // Create a random x coordinate for the delivery in the drop range. float dropPosX = Random.Range(dropRangeLeft, dropRangeRight); // Create a position with the random x coordinate. Vector3 dropPos = new Vector3(dropPosX, 15f, 1f); // If the player's health is above the high threshold... if (playerHealth.health >= highHealthThreshold) { // ... instantiate a bomb pickup at the drop position. Instantiate(pickups [0], dropPos, Quaternion.identity); } // Otherwise if the player's health is below the low threshold... else if (playerHealth.health <= lowHealthThreshold) { // ... instantiate a health pickup at the drop position. Instantiate(pickups [1], dropPos, Quaternion.identity); } // Otherwise... else { // ... instantiate a random pickup at the drop position. int pickupIndex = Random.Range(0, pickups.Length); Instantiate(pickups [pickupIndex], dropPos, Quaternion.identity); } }