Hey guys. So I started to work on creating a gun for an upcoming “Madness-esq” playground. The actual script I’m making is serving more as a template, then onto specifics with special weapons. The script is being designed to have recoil, bullet projectile, sounds, lighting effects, ammo count system, and all that fun stuff. Most of the variables have been left up to the inspector to input the values for easy attachment to other guns. A lot of this stuff has not been implemented yet.
Here’s the issues I am facing right now:
-
When using " ‘reloadSpeed’ = ‘gunShoot’.length (gunShoot as audio clip) , the reload time is greatly increased. Instead I have to set the exact reload speed in the inspector for it to work. I’d like this to be set to the clips length, so I don’t have to fiddle with timing. Instead I can have the reload speed (and other variables) set to the length of the clip. I feel like this is an easy one, but I’m not figuring it out.
-
When reloading, ammo is not being taken out of the reserve. I can’t wrap my head around that aspect just yet and I can’t properly put it into syntax. Basically, all it does is mimic being taken out of the reserve by keeping track of shots fired, then subtracting the current reserve amount by the shots fired prior to reloading. This obviously has flaws, as you can go into the negatives with reserve ammo.
Here is my script so far. I have the new reload sound code commented out, and replaced with the version that works.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GeneralGuns : MonoBehaviour
{
public Text currentClip;
public Text currentReserve;
public Transform gunMuzzle;
public Rigidbody2D gunRigid;
public GameObject gunProjectile;
public GameObject bulletShell;
public GameObject flashSprite;
public AudioSource gunPref;
public AudioClip gunShoot;
public AudioClip gunReload;
public int curClip;
public int curReserve;
private int lastClip;
public int maxClip;
private int curRes;
public float rateOfFire;
[SerializeField] private float reloadSpeed;
public float recoilMin;
public float recoilMax;
public float randRecoil;
public bool isSemi;
public bool isFull;
private bool isReloading;
void Start()
{
gunRigid = GetComponent<Rigidbody2D> ();
lastClip = 0;
isReloading = false;
//reloadSpeed = gunShoot.length;
}
void Update()
{
currentClip.text = "" + curClip;
currentReserve.text = "" + curReserve;
Fire ();
ManualReload ();
}
void Fire()
{
if (Input.GetKeyDown (KeyCode.Space) && !isReloading)
{
if (curClip > 0)
{
curClip--;
lastClip++;
//Diff between clip and reserve(?)
gunPref.PlayOneShot (gunShoot);
flashSprite.SetActive (true);
}
else
{
StartCoroutine (Reload ());
}
}
else
{
flashSprite.SetActive (false);
}
}
void ManualReload()
{
if (Input.GetKeyDown (KeyCode.R) && !isReloading)
{
StartCoroutine (Reload ());
}
}
IEnumerator Reload()
{
if (curClip != maxClip)
{
isReloading = true;
gunPref.PlayOneShot (gunReload);
//yield return new WaitForSecondsRealtime (reloadSpeed);
yield return new WaitForSeconds(reloadSpeed);
isReloading = false;
curClip = maxClip;
curReserve = curReserve - lastClip;
lastClip = 0;
Debug.Log ("Reload Time = " + gunReload.length);
}
}
}