Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;//for player healthbar
  3. using System.Collections;
  4.  
  5. public class HurtOnCollisionWithNamed : MonoBehaviour
  6. {
  7. public string containsSubstring;
  8. public GameObject explosionPrefab;
  9.  
  10. public Slider playerHealthSlider;//for player healthbar
  11.  
  12. public void OnTriggerEnter2D(Collider2D coll)
  13. {
  14. DecreaseHealth(coll);
  15. }
  16.  
  17. public void OnCollisionEnter2D(Collision2D coll)
  18. {
  19. DecreaseHealth(coll.collider);
  20. }
  21.  
  22. public void DecreaseHealth(Collider2D coll)
  23. {
  24. if (coll.name.Contains(containsSubstring))
  25. {
  26. GetComponent<PlayerHealth>().health--;
  27. GameObject explosion = (GameObject)Instantiate(explosionPrefab, transform.position, transform.rotation);
  28. explosion.GetComponent<ParticleSystem>().startColor = Color.red;
  29. playerHealthSlider.value = GetComponent<PlayerHealth>().health;//for player healthbar
  30. }
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement