- Home /
Reload clips on Gun-script not applied properly
I'm trying to make my gun script reload similar to that of current FPS in which if a player shoots 1 bullet and the player reloads the gun then it will reload the gun to the full clip amount and take into account of the 1 ammo left so that on the final clip of ammo there'd be 1 ammo less than on a normal clip.
For example you have 35 ammo with 7 per clip. You shoot say 2 times and then reload meaning your total ammo is 33. You will have a full clip until your last clip where there will be 5 ammo in the clip instead of the full 7 due to the already spent ammo.
I've managed to make it so my gun reloads after all 7 ammo per clip has been used up and then reloads with another 7 ammo however I have been unable to figure out how to implement the above problem. Anyone able to help me out to figuring out what I need to do, to do this?
RayShoot Script
public int clip = 80;
public int bulletsPerClip = 60;
public int bulletsLeft = 0;
public int particleSpeed = 200000;
public float damage = 10.0f;
public float range = 1000.0f;
public float reloadTime = 1.6f;
public float force = 1000;
public float fireSpeed = 15.0f;
[HideInInspector]
public float waitTillFire = 0;
public GameObject bullet;
public GameObject bulletSpawn;
public float shootTimer = 0.0f;
public float shootCooler = 0.1f;
public float muzzleFlashCooler = 0.1f;
public float muzzleFlashTimer = 0.0f;
public float keyCooler = 0.5f;
public float keyTimer = 0.0f;
// public GameObject light1;
// public GameObject light2;
// public GameObject light3;
public AudioClip reloadSound;
public AudioClip shootSound;
public AudioClip emptyGunSound;
public ParticleEmitter muzzleFlash;
// public ParticleEmitter hitParticles;
// Use this for initialization
void Start ()
{
bulletsLeft = bulletsPerClip;
// hitParticles.emit = false;
muzzleFlash.gameObject.SetActive(true);
muzzleFlash.emit = false;
}
// Update is called once per frame
void Update ()
{
if (keyTimer > 0)
{
keyTimer -= Time.deltaTime;
}
if (keyTimer < 0)
{
keyTimer = 0;
}
if (muzzleFlashTimer > 0)
{
muzzleFlashTimer -= Time.deltaTime;
MuzzleFlashShow();
}
if (muzzleFlashTimer < 0)
{
muzzleFlashTimer = 0;
}
if (shootTimer > 0)
{
shootTimer -= Time.deltaTime;
}
if (shootTimer < 0)
{
shootTimer = 0;
}
if (keyTimer == 0)
{
if (Input.GetMouseButton (0) && bulletsLeft > 0)
{
if (shootTimer == 0)
{
PlayShootAudio();
RayFire();
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
shootTimer = shootCooler;
keyTimer = keyCooler;
}
if (muzzleFlashTimer == 0)
{
muzzleFlashTimer = muzzleFlashCooler;
MuzzleFlashShow();
}
}
else if (Input.GetMouseButtonDown (0) && clip == 0)
{
PlayEmptyAudio();
shootTimer = shootCooler;
keyTimer = keyCooler;
}
if (Input.GetKey(KeyCode.R))
{
Reload();
shootTimer = shootCooler;
keyTimer = keyCooler;
}
}
}
void MuzzleFlashShow()
{
if (muzzleFlashTimer > 0)
{
muzzleFlash.emit = false;
// light1.SetActive(false);
// light2.SetActive(false);
// light3.SetActive(false);
}
if (muzzleFlashTimer == muzzleFlashCooler)
{
muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360 * particleSpeed, Vector3.forward);
muzzleFlash.emit = true;
// light1.SetActive(true);
// light2.SetActive(true);
// light3.SetActive(true);
}
}
void RayFire()
{
GameObject.Find("CQAssaultRifle").animation.Play("GunFiring"); // First name is for name of object that will play your animation and second name is what you animation is called
RaycastHit hit;
Vector3 directionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, directionRay * range, Color.yellow);
if (Physics.Raycast(transform.position, directionRay, out hit, range))
{
if (hit.rigidbody)
{
// if (hitParticles)
// {
// hitParticles.transform.position = hit.point;
// hitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
// hitParticles.Emit();
hit.rigidbody.AddForceAtPosition(directionRay * force, hit.point);
// hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
// }
}
}
bulletsLeft--;
if (bulletsLeft < 0)
{
bulletsLeft = 0;
}
if (bulletsLeft == 0)
{
Reload();
}
}
void PlayShootAudio()
{
audio.PlayOneShot(shootSound);
}
void PlayReloadAudio()
{
audio.PlayOneShot(reloadSound);
}
void PlayEmptyAudio()
{
audio.PlayOneShot(emptyGunSound);
}
void Reload()
{
if (clip > 0)
{
StartCoroutine(ReloadSpeed());
clip--;
}
}
IEnumerator ReloadSpeed()
{
if (clip > 0)
{
// GameObject.Find("").animation.Play(""); // First name is for name of object that will play your animation and second name is what you animation is called
PlayReloadAudio();
yield return new WaitForSeconds(reloadTime);
//if (clip > 0)
//{
bulletsLeft = bulletsPerClip;
//}
}
else if (clip < 0)
{
PlayEmptyAudio();
}
}
Answer by ppoomi · Apr 07, 2015 at 12:13 PM
if (clip > 0) { StartCoroutine(ReloadSpeed()); clip--;
That doesn't tell me anything. All you did was just write a section of my code and didn't tell me what to do with it bar just advertising software to buy.
Your answer
Follow this Question
Related Questions
How can i make a ray cast take health from enemies 2 Answers
Raycast Projectile w/ Physics 2 Answers
realistic reload system? 1 Answer
Make bullet launch at center of screen 2 Answers