emlattimore

Pawnship Attack

Apr 30th, 2019
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PawnshipAttack : MonoBehaviour
  6. {
  7.     public int attackDamage = 5;      // The pawn ship's damage that it deals.
  8.  
  9.     public GameObject bluePlayer;     // References the blue player game object.
  10.     public GameObject pawnShip;       // References the pawn ship game object.
  11.     PlayerHealth playerHealth;        // References the PlayerHealth script.
  12.     public bool playerInRange;        // Determines if the player is in range or not.
  13.  
  14.     private void Awake()
  15.     {
  16.         bluePlayer = GameObject.FindGameObjectWithTag("Player");   // Finds the game object that is assigned the tag "Player".
  17.         playerHealth = GetComponent<PlayerHealth>();               // References the PlayerHealth script.
  18.     }
  19.  
  20.     private void OnTriggerEnter2D(Collider2D other)
  21.     {
  22.         if (other.gameObject == bluePlayer)             // If the pawn ship's collider is in range of the player's collider...
  23.         {
  24.             playerInRange = true;                       // ... the player is in range.
  25.         }
  26.     }
  27.  
  28.     private void OnTriggerExit2D(Collider2D other)
  29.     {
  30.         if (other.gameObject == bluePlayer)             // If the pawn ship's collider is not in range of the player's collider...
  31.         {
  32.             playerInRange = false;                      // ... the player is not in range.
  33.         }
  34.     }
  35.  
  36.     private void Update()
  37.     {
  38.         if (playerInRange)            // If the player is in range...
  39.         {
  40.             Attack();                 // ... attack.
  41.         }
  42.     }
  43.  
  44.     void Attack()
  45.     {
  46.         if (playerHealth.currentHealth > 0)             // If the player's health is greater than 0 and the game object is the blue player...
  47.         {
  48.             playerHealth.TakeDamage(attackDamage);      // ... deal damage equivalent to the attackDamage specified earlier in this script.
  49.         }
  50.         Destroy(pawnShip);
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment