Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PawnshipAttack : MonoBehaviour
- {
- public int attackDamage = 5; // The pawn ship's damage that it deals.
- public GameObject bluePlayer; // References the blue player game object.
- public GameObject pawnShip; // References the pawn ship game object.
- PlayerHealth playerHealth; // References the PlayerHealth script.
- public bool playerInRange; // Determines if the player is in range or not.
- private void Awake()
- {
- bluePlayer = GameObject.FindGameObjectWithTag("Player"); // Finds the game object that is assigned the tag "Player".
- playerHealth = GetComponent<PlayerHealth>(); // References the PlayerHealth script.
- }
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.gameObject == bluePlayer) // If the pawn ship's collider is in range of the player's collider...
- {
- playerInRange = true; // ... the player is in range.
- }
- }
- private void OnTriggerExit2D(Collider2D other)
- {
- if (other.gameObject == bluePlayer) // If the pawn ship's collider is not in range of the player's collider...
- {
- playerInRange = false; // ... the player is not in range.
- }
- }
- private void Update()
- {
- if (playerInRange) // If the player is in range...
- {
- Attack(); // ... attack.
- }
- }
- void Attack()
- {
- if (playerHealth.currentHealth > 0) // If the player's health is greater than 0 and the game object is the blue player...
- {
- playerHealth.TakeDamage(attackDamage); // ... deal damage equivalent to the attackDamage specified earlier in this script.
- }
- Destroy(pawnShip);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment