Advertisement
EmperorDuck

Flying Enemy

Nov 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class FlyingEnemy : MonoBehaviour
  6. {
  7.  
  8. public float flyHealth = 1;
  9. private float spearDamage = 1;
  10. private float leftMove = 2.0f;
  11. private float rightMove = 2.0f;
  12. private float speed = 5.0f;
  13. private int direction = 1;
  14. private Vector2 movement;
  15. public Rigidbody potion;
  16. public GameObject otherEnemy;
  17.  
  18.  
  19.  
  20. // Update is called once per frame
  21. void Update()
  22. {
  23.  
  24. while (flyHealth == 1)
  25. {
  26. if (transform.position.x > rightMove)
  27. {
  28. direction = -1;
  29. }
  30.  
  31. else if (transform.position.x > leftMove)
  32. {
  33. direction = 1;
  34. }
  35.  
  36. movement = Vector2.right * direction * speed * Time.deltaTime;
  37. transform.Translate(movement);
  38. if (Random.Range(0f, 1f) == 1)
  39. {
  40.  
  41. Rigidbody potionInstance;
  42. potionInstance = Instantiate(potion) as Rigidbody;
  43. potionInstance.AddForce(Vector3.down * 5000);
  44. }
  45. }
  46. }
  47. private void OnTriggerEnter2D(Collider2D other)
  48. {
  49. if (other.tag == "Spear")
  50. {
  51. flyHealth -= spearDamage;
  52. }
  53.  
  54. if (flyHealth <= 0)
  55. {
  56. Destroy(otherFlyEnemy);
  57. if (Random.Range(0f, 1f) == 1)
  58. {
  59. Rigidbody potionInstance;
  60. potionInstance = Instatiate(potion) as Rigidbody;
  61. potionInstance.addForce(Vector3.down * 5000);
  62. }
  63. }
  64.  
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement