void OnTriggerEnter2D(Collider2D other) { // If the player enters the trigger zone... if (other.tag == "Player") { // Get a reference to the player health script. DogHealth playerHealth = other.GetComponent <DogHealth>(); // 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; } }
// Use this for initialization void Start() { player = GameObject.FindGameObjectWithTag ("Player"); planner = this.GetComponent<GoalPlanner> (); health = this.GetComponent<DogHealth> (); availableActions = new HashSet<Action> (); GetActions (); }
private DogHealth playerHealth; // Reference to the PlayerHealth script. void Awake() { // Setting up the reference. playerHealth = GameObject.FindGameObjectWithTag("Player").GetComponent <DogHealth>(); }