using System.Collections; using System.Collections.Generic; using UnityEngine; /* -------------------------------------- Project: Programing assessment Standard: 91906 (AS3.7) v.1 School: Tauranga Boys' College Author: Rauputu Noah Phizacklea Date: August 2024 Unity: 2021.3.18f --------------------------------------- */ public class Bullet : MonoBehaviour { public float speed; // Speed of the bullet public float lifeTime; // Time before the bullet is destroyed public float distance; // Distance the bullet can travel before hitting something public int damage; // Damage dealt by the bullet public LayerMask solidLayers; // Layers considered solid (for collision detection) public GameObject destroyEffect; // Effect to play when the bullet is destroyed void Start() { // Schedule the bullet to be destroyed after its lifetime Invoke("DestroyBullet", lifeTime); } void Update() { // Cast a ray to detect collisions with solid layers RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, solidLayers); if (hitInfo.collider != null) { // Check if the hit object has an EnemyHealth component and apply damage if (hitInfo.collider.CompareTag("Enemy")) { hitInfo.collider.GetComponent().TakeDamage(damage); } // Check if the hit object has a BossHealth component and apply damage if (hitInfo.collider.CompareTag("Boss")) { hitInfo.collider.GetComponent().TakeDamage(damage); } // Destroy the bullet if it hits something DestroyBullet(); } // Move the bullet forward based on its speed transform.Translate(Vector2.right * speed * Time.deltaTime); } void DestroyBullet() { // Instantiate the destroy effect at the bullet's position Instantiate(destroyEffect, transform.position, Quaternion.identity); // Destroy the bullet game object Destroy(gameObject); } }