using System.Collections; using System.Collections.Generic; using UnityEngine; public class Projectile : MonoBehaviour { [SerializeField] private float moveSpeed = 22f; // Speed at which the projectile moves [SerializeField] private GameObject particleOnHitPrefabVFX; // Visual effect to instantiate upon hitting a target [SerializeField] private bool isEnemyProjectile = false; // Flag to determine if this projectile belongs to an enemy [SerializeField] private float projectileRange = 10f; // Maximum distance the projectile can travel private Vector3 startPosition; // Initial position of the projectile private void Start() { startPosition = transform.position; // Store the starting position to calculate travel distance } private void Update() { MoveProjectile(); // Handle projectile movement DetectFireDistance(); // Check if the projectile has exceeded its range } // Updates the maximum range the projectile can travel public void UpdateProjectileRange(float projectileRange){ this.projectileRange = projectileRange; } // Updates the speed at which the projectile moves public void UpdateMoveSpeed(float moveSpeed) { this.moveSpeed = moveSpeed; } // Handles collision detection and damage application to enemies or players private void OnTriggerEnter2D(Collider2D other) { EnemyHealth enemyHealth = other.gameObject.GetComponent(); Indestructible indestructible = other.gameObject.GetComponent(); PlayerHealth player = other.gameObject.GetComponent(); // Check if the projectile hits a valid target (enemy, indestructible object, or player) if (!other.isTrigger && (enemyHealth || indestructible || player)) { // Handle damage and effects if the projectile hits an appropriate target if ((player && isEnemyProjectile) || (enemyHealth && !isEnemyProjectile)) { player?.TakeDamage(1, transform); // Apply damage to the player Instantiate(particleOnHitPrefabVFX, transform.position, transform.rotation); // Spawn hit effect Destroy(gameObject); // Destroy the projectile } else if (!other.isTrigger && indestructible) { Instantiate(particleOnHitPrefabVFX, transform.position, transform.rotation); // Spawn hit effect on indestructible objects Destroy(gameObject); // Destroy the projectile } } } // Destroys the projectile if it exceeds its designated travel range private void DetectFireDistance() { if (Vector3.Distance(transform.position, startPosition) > projectileRange) { Destroy(gameObject); } } // Moves the projectile forward in the direction it is facing private void MoveProjectile() { transform.Translate(Vector3.right * Time.deltaTime * moveSpeed); } }