/* * ------------------------------------------------------------------------ * Description: This class handles the script for shooting * Author: Eli Simpkins-Simmonds * ------------------------------------------------------------------------ */ using UnityEngine; public class Shooter : MonoBehaviour { [SerializeField] GameObject projectilePrefab; [SerializeField] float projectileSpeed = 10f; [SerializeField] float projectileLifetime = 5f; public bool isFiring; void Update() { if (isFiring) { Fire(); isFiring = false; // Reset firing flag after firing a single bullet } } void Fire() { // Instantiate the projectile prefab GameObject instance = Instantiate(projectilePrefab, transform.position, Quaternion.identity); // Attach the collision handler script to handle collisions ProjectileCollisionHandler collisionHandler = instance.AddComponent(); // Set the damage amount for the projectile collisionHandler.damage = 20; // Example: Set the damage amount here // Get the Rigidbody2D component from the instantiated projectile Rigidbody2D rb = instance.GetComponent(); if (rb != null) { // Ensure continuous collision detection for high-speed projectiles rb.collisionDetectionMode = CollisionDetectionMode2D.Continuous; // Set the projectile direction based on the player's facing direction float direction = transform.localScale.x; // Assumes player facing right is positive x scale rb.velocity = new Vector2(direction * projectileSpeed, 0f); } // Destroy the projectile after its lifetime Destroy(instance, projectileLifetime); } }