Guest User

Enemy Scripts

a guest
Mar 18th, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. //==Scripts Used for Enemies==//
  2.  
  3. public class DestroyOnContact : MonoBehaviour {
  4. //Declare a scorevalue for the object this is attached to
  5. public int scoreVal;
  6.  
  7. //Call the GameController Script
  8. private GameController gc;
  9.  
  10. //When another object tagged with player collides with this, destroy both objects
  11. public float count;
  12. void Awake (){
  13. GameObject gco = GameObject.FindGameObjectWithTag("GameController");
  14. gc = gco.GetComponent<GameController>();
  15. count = 0;
  16. }
  17.  
  18. void OnTriggerEnter2D(Collider2D other){
  19. if (other.CompareTag ("Boundary") || other.CompareTag ("Enemy")) {
  20. return;
  21. }
  22. if (other.CompareTag ("Weapon Shot") && count < 3) {
  23. count = count + 1;
  24. Destroy (other.gameObject);
  25. return;
  26. }
  27. else if (count > 2) {
  28. Destroy (other.gameObject);
  29. //Rather than duplicating code here, we should write and call a function
  30. Destroy (gameObject);
  31. gc.AddScore (scoreVal);
  32. }
  33. }
  34. }
  35.  
  36. //===============//
  37.  
  38. public class Enemy1Control : MonoBehaviour {
  39. //What we want to do: Have the enemy move forward, only 1 direction; Have the enemy shoot; Have the enemy shoot at the player's position
  40. //Have the enemy move
  41. public GameObject shot;
  42. public Rigidbody2D rb;
  43. public float speed;
  44.  
  45. //Check that the player is in the game
  46. public GameObject thePlayer;
  47. //Check that the Player isn't dead
  48. public bool thePlayerLives;
  49.  
  50. //Have the enemy shoot at the player's position; the movement code is in the shot itself so what we do is instantiate it at the firing point
  51. public GameObject fireShot;
  52. //Where are we shooting from?
  53. public Transform fireMouth;
  54. //How long to wait between firing
  55. public float rateOfFire;
  56. public float fireDelay;
  57.  
  58. //Call the Player Lives function just in case the shot is instantiated after the player dies
  59. private GameController gc;
  60.  
  61. void Awake(){
  62. GameObject gco = GameObject.FindGameObjectWithTag("GameController");
  63. gc = gco.GetComponent<GameController>();
  64. rb = GetComponent<Rigidbody2D>();
  65. rb = GetComponent<Rigidbody2D>();
  66. rb.velocity = (-transform.right * speed);
  67. gc.Invoke ("CheckLIves",0);
  68. StartCoroutine ("Shooter");
  69. }
  70.  
  71. void FixedUpdate () {
  72. gc.Invoke ("CheckLIves",0);
  73. }
  74. //Coroutine for spawning and shooting objects
  75. IEnumerator Shooter() {
  76. while (gc.thePlayerLives) {
  77. // print ("Coroutine Started");
  78. yield return new WaitForSeconds (fireDelay);
  79. gc.Invoke ("CheckLIves",0);
  80. if (gc.thePlayerLives) {
  81. for (int i = 0; i < 1; i++) {
  82. rb.velocity = Vector2.zero;
  83. yield return new WaitForSeconds (fireDelay);
  84. Instantiate (fireShot, fireMouth.position, fireMouth.rotation);
  85. rb.velocity = (-transform.right * speed);
  86. yield return new WaitForSeconds(rateOfFire);
  87. }
  88. }
  89. yield return new WaitForSeconds(rateOfFire);
  90. if (!gc.thePlayerLives) {
  91. rb.velocity = (-transform.right * speed);
  92. break;
  93. }
  94. }
  95. }
  96. }
  97.  
  98. //===============//
  99.  
  100. public class Enemy2Control : MonoBehaviour {
  101. public Rigidbody2D rb;
  102. public float speed;
  103.  
  104. //Have the enemy shoot at the player's position; the movement code is in the shot itself so what we do is instantiate it at the firing point
  105. public GameObject fireShots;
  106. //Where are we shooting from?
  107. public Transform fireBig;
  108. public Transform fireLong;
  109. //How long to wait between firing
  110. public float rateOfFireBig;
  111. public float rateOfFireLong;
  112. public float fireDelay;
  113.  
  114. void Start () {
  115. rb = GetComponent<Rigidbody2D>();
  116. rb.velocity = (-transform.right * speed);
  117. InvokeRepeating ("BigGuns",fireDelay,rateOfFireBig);
  118. InvokeRepeating ("LongGuns",fireDelay,rateOfFireLong);
  119. }
  120.  
  121. void BigGuns() {
  122. if (Time.time > rateOfFireBig) {
  123. Instantiate (fireShots, fireBig.position, fireBig.rotation);
  124.  
  125. }
  126. }
  127. void LongGuns() {
  128. if (Time.time > rateOfFireLong) {
  129. Instantiate (fireShots, fireLong.position, fireLong.rotation);
  130. }
  131. }
  132. }
  133.  
  134.  
  135. //===============//
  136.  
  137.  
  138. public class Enemy2DestroyOnContact : MonoBehaviour {
  139.  
  140. //Declare a scorevalue for the object this is attached to
  141. public int scoreVal;
  142.  
  143. //Call the GameController Script
  144. private GameController gc;
  145.  
  146. void Awake (){
  147. GameObject gco = GameObject.FindGameObjectWithTag("GameController");
  148. gc = gco.GetComponent<GameController>();
  149. }
  150.  
  151. void OnTriggerEnter2D(Collider2D other){
  152. if (other.CompareTag ("Weapon Shot")) {
  153. Destroy (other.gameObject);
  154. Destroy (gameObject);
  155. gc.AddScore (scoreVal);
  156. }
  157. }
  158. }
  159.  
  160.  
  161. //===============//
  162.  
  163.  
  164. public class EnemyShot2Control : MonoBehaviour {
  165.  
  166. public GameObject shot;
  167. public Rigidbody2D rb;
  168. public float speed;
  169.  
  170. // Use this for initialization
  171. void Start () {
  172. rb = GetComponent<Rigidbody2D>();
  173. rb.velocity = ((-transform.right) * speed);
  174. }
  175.  
  176. }
  177.  
  178.  
  179. //===============//
  180.  
  181.  
  182. public class EnemyShotScript : MonoBehaviour {
  183.  
  184. public GameObject shot;
  185. public Rigidbody2D rb;
  186. public float speed;
  187.  
  188. public GameObject target;
  189. //private Transform shotTransform;
  190. private Vector3 targetPOS;
  191.  
  192. //Call the Player Lives function just in case the shot is instantiated after the player dies
  193. private GameController gc;
  194.  
  195. void Awake(){
  196. GameObject gco = GameObject.FindGameObjectWithTag("GameController");
  197. gc = gco.GetComponent<GameController>();
  198. rb = GetComponent<Rigidbody2D>();
  199. }
  200.  
  201. void Start () {
  202. if (gc.thePlayerLives) {
  203. BeginShot ();
  204. } else
  205. Destroy (gameObject);
  206. }
  207.  
  208. void BeginShot(){
  209. //Theoretically this finds the position of the player
  210. target = GameObject.FindGameObjectWithTag("Player");
  211. targetPOS = new Vector3 (target.transform.position.x, target.transform.position.y, target.transform.position.z);
  212. transform.rotation = Quaternion.FromToRotation (transform.up, targetPOS - transform.position) * transform.rotation;
  213. rb.velocity = (transform.up * speed);
  214. //Taken from Unity Answers, we need to break this down and figure out why it works
  215. //transform.rotation = Quaternion.FromToRotation (transform.up, targetPOS - transform.position) * transform.rotation;
  216.  
  217. }
  218. }
Add Comment
Please, Sign In to add comment