Advertisement
Guest User

Untitled

a guest
Sep 18th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Enemy : MonoBehaviour
  6. {
  7. private SpriteRenderer sprite;
  8.  
  9. public GameObject hitParticle;
  10. public GameObject hitBox;
  11.  
  12. private Vector3 currentTarget;
  13. public Transform pointA, pointB;
  14.  
  15. public int health;
  16. public float speed;
  17.  
  18. bool canBeDamaged = true;
  19.  
  20. private void Start()
  21. {
  22. sprite = GetComponentInChildren<SpriteRenderer>();
  23. }
  24.  
  25. private void Update()
  26. {
  27. Movement();
  28. }
  29.  
  30. public void Movement()
  31. {
  32. if (transform.position == pointA.position)
  33. {
  34. currentTarget = pointB.position;
  35. sprite.flipX = false;
  36. }
  37.  
  38. else if (transform.position == pointB.position)
  39. {
  40. currentTarget = pointA.position;
  41. sprite.flipX = true;
  42. }
  43.  
  44. transform.position = Vector3.MoveTowards(transform.localPosition, currentTarget, speed * Time.deltaTime);
  45. }
  46.  
  47. private void OnCollisionEnter2D(Collision2D other)
  48. {
  49. if (other.gameObject.CompareTag("Player"))
  50. {
  51. if(canBeDamaged)
  52. {
  53. Instantiate(hitParticle,transform.position, transform.rotation);
  54. health--;
  55. StartCoroutine(CanBeDamagedRoutine());
  56. canBeDamaged = false;
  57. }
  58.  
  59. if (health < 1)
  60. {
  61. Destroy(this.gameObject);
  62. }
  63. }
  64. }
  65.  
  66. IEnumerator CanBeDamagedRoutine()
  67. {
  68. yield return new WaitForSeconds(0.6f);
  69. canBeDamaged = true;
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement