Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;//for player healthbar
- using System.Collections;
- public class HurtOnCollisionWithNamed : MonoBehaviour
- {
- public string containsSubstring;
- public GameObject explosionPrefab;
- public Slider playerHealthSlider;//for player healthbar
- public void OnTriggerEnter2D(Collider2D coll)
- {
- DecreaseHealth(coll);
- }
- public void OnCollisionEnter2D(Collision2D coll)
- {
- DecreaseHealth(coll.collider);
- }
- public void DecreaseHealth(Collider2D coll)
- {
- if (coll.name.Contains(containsSubstring))
- {
- GetComponent<PlayerHealth>().health--;
- GameObject explosion = (GameObject)Instantiate(explosionPrefab, transform.position, transform.rotation);
- explosion.GetComponent<ParticleSystem>().startColor = Color.red;
- playerHealthSlider.value = GetComponent<PlayerHealth>().health;//for player healthbar
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement