It helps to rewrite code in a meaningful (readable) way, consider:
// required for IEnuemrator
using System.Collections;
using UnityEngine;
public class Test : MonoBehaviour
{
// Use meaningful TitleCase variables for public things!
public float BaseDamage = 0f;
public float FireRate = .5f; // means twice per second, lower number is faster
public float ReloadTime = 2f;
public LayerMask ValidTargets;
// Put all public variables first, it's easier to read.
public int ClipContents = 20;
public int ClipCapacity = 20;
public int Stock = 40;
// Private variables get camelCase
private float fireDelay = 0f;
private Transform firePoint;
// Putting stuff inside a `region` block is optional, but cleans up files
#region Unity
private void Awake()
{
// Unity is pretty smart about this, and will throw an error anyways
// You don't have to have your own error checking for assignment.
firePoint = transform.Find("firePoint");
}
// We implement the fireDelay counter in FixedUpdate so it's run exactly 20 times a second
private void FixedUpdate()
{
// The delay value reduces towards 0 over time
if (fireDelay > 0)
fireDelay--;
}
// Whereas things inside Update are tied to your FRAME RATE
private void Update()
{
// When fireDelay reaches 0, we can fire again.
if (fireDelay <= 0)
if (Input.GetButtonDown("Fire1"))
{
// Reset fireDelay when we fire
fireDelay = FireRate * 20f;
Shoot();
}
if (Input.GetButtonDown("Reload"))
{
// Prevent firing for at least reloadTime
fireDelay = ReloadTime * 20f;
// Call the reload IEnumerator
StartCoroutine(Reload());
}
}
#endregion
private IEnumerator Reload()
{
// Until our clip is full, use bullets to fill it.
while (ClipContents < ClipCapacity)
{
// We ran out of bullets to reload
// yield break cancels an IEnumerator entirely
if (Stock == 0) yield break;
ClipContents++;
Stock--;
// This lets us reload 20 bullets per second
yield return new WaitForFixedUpdate();
}
// IEnumerator must `yield return null` at the end.
yield return null;
}
private void Shoot()
{
// Make sure our clip isn't empty
if (ClipContents == 0)
{
// This is where you might add a "firingEmpty" sound
return;
}
// Unity expresses things in 3D even if you're not..
Vector2 point = new Vector2(Input.mousePosition.x, Input.mousePosition.y); // Are you sure it's not z ?
// Check where that is in world position
point = Camera.main.ScreenToWorldPoint(point);
Vector2 reticlePos = new Vector2(firePoint.position.x, firePoint.position.y); // are you sure it's not z ?
// "Fire the bullet" beyond this point
// Consider actually spawning a bullet prefab
// - then you can check collision on the bullet
ClipContents--;
// Arguments are "origin", "direction", "distance" and "layerMask" in that order
RaycastHit2D hit = Physics2D.Raycast(reticlePos, point - reticlePos, 100, ValidTargets);
if (hit.collider != null && hit.transform != null)
// I'm assuming you meant to destroy the target, not yourself
Destroy(hit.transform.gameObject);
}
}