using Unity.VisualScripting; using UnityEngine; public class WeaponsController : MonoBehaviour { public float range; public Transform cam; public LayerMask validLayers; public GameObject impactEffect, damageEffect, muzzleFlare; // Reference to the impact effect and muzzle flare prefabs public float timeBetweenShots = .2f, shotCounter, flareDisplayTime = 0.1f,damageAmount = 15f; // Time between shots and flare display duration public bool canAutoFire; // Flag to enable or disable auto fire private float flareCounter; public int currentAmmo = 100, clipSize = 20, remainingAmmo = 300, pickupAmount; // Current ammo count, clip size, and remaining ammo private UIController uiCon; // Reference to the UIController script for updating ammo text // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { muzzleFlare.SetActive(false); uiCon = FindFirstObjectByType(); // Find and reference the UIController in the scene // Initialize ammo counts int ammoToLoad = Mathf.Min(clipSize, remainingAmmo + currentAmmo); currentAmmo = ammoToLoad; remainingAmmo = (remainingAmmo + currentAmmo) - ammoToLoad; uiCon.UpdateAmmoText(currentAmmo, remainingAmmo); // Update UI on start } // Update is called once per frame void Update() { if (flareCounter > 0) { flareCounter -= Time.deltaTime; // Decrease flare counter over time if (flareCounter <= 0) { muzzleFlare.SetActive(false); // Deactivate muzzle flare effect when counter reaches zero } } } public void Shoot() { if (currentAmmo <= 0) return; // Check if there is ammo left before shooting { RaycastHit hit; if (Physics.Raycast(cam.position, cam.forward, out hit, range, validLayers)) { Debug.Log("Hit: " + hit.collider.name); if (hit.collider.CompareTag("Enemy")) { Instantiate(damageEffect, hit.point, Quaternion.identity); // Create damage effect at enemy hit point hit.transform.GetComponent().TakeDamage(damageAmount); // Call TakeDamage method on the enemy's health script } else { Instantiate(impactEffect, hit.point, Quaternion.identity); // Create impact effect at hit point } } muzzleFlare.SetActive(true); // Activate muzzle flare effect flareCounter = flareDisplayTime; // Reset flare counter shotCounter = timeBetweenShots; // Reset shot counter for auto fire currentAmmo--; // Decrease ammo count by 1 when shooting uiCon.UpdateAmmoText(currentAmmo, remainingAmmo); // Update the ammo text in the UI } } public void ShootHeld() { shotCounter -= Time.deltaTime; // Decrease shot counter over time if (shotCounter <= 0 && canAutoFire) // Check if shot counter is zero and auto fire is enabled { Shoot(); // Call Shoot method for continuous shooting shotCounter = timeBetweenShots; // Reset shot counter for next shot } } public void Reload() { int ammoNeeded = clipSize - currentAmmo; // Calculate ammo needed to fill the clip if (ammoNeeded > 0 && remainingAmmo > 0) // Check if reload is needed and possible { int ammoToReload = Mathf.Min(ammoNeeded, remainingAmmo); // Determine how much ammo to reload currentAmmo += ammoToReload; // Add ammo to the current clip remainingAmmo -= ammoToReload; // Subtract reloaded ammo from remaining ammo } uiCon.UpdateAmmoText(currentAmmo, remainingAmmo); // Update the ammo text in the UI } public void GetAmmo() { remainingAmmo += pickupAmount; // Increase remaining ammo by the pickup amount uiCon.UpdateAmmoText(currentAmmo, remainingAmmo); // Update the ammo text in the UI } }