Files
CartoonFPS/Assets/Scripts/WeaponsController.cs

96 lines
4.2 KiB
C#
Raw Normal View History

2025-08-05 09:30:40 +01:00
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
2025-08-05 17:31:28 +01:00
public float timeBetweenShots = .2f, shotCounter, flareDisplayTime = 0.1f, damageAmount = 15f; // Time between shots and flare display duration
2025-08-05 09:30:40 +01:00
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<UIController>(); // Find and reference the UIController in the scene
2025-08-05 17:31:28 +01:00
Reload(); // Initialize ammo counts
SetWeapon(new Weapon()); // Set the initial weapon (this could be modified to select a specific weapon)
2025-08-05 09:30:40 +01:00
}
// 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"))
{
2025-08-05 15:34:40 +01:00
Instantiate(damageEffect, hit.point, Quaternion.identity); // Create damage effect at enemy hit point
hit.transform.GetComponent<EnemyController>().TakeDamage(damageAmount); // Call TakeDamage method on the enemy's health script
2025-08-05 09:30:40 +01:00
}
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
}
2025-08-05 17:31:28 +01:00
public void SetWeapon(Weapon newWeapon)
{
// Logic to switch to a new weapon
// This could involve changing the active weapon model, updating UI, etc.
Debug.Log("Switched to weapon: " + newWeapon.name);
}
2025-08-05 09:30:40 +01:00
}