- Home /
Reload will not work... coders needed.
When I run completely out of ammo it calls a reload function and the gun reloads. When I press 'R' to reload when rounds have been fired it's supposed to call the same function, but it doesn't work for some reason, I'm not an expert but I did create this script by myself and it's beyond me why this won't work, or maybe I'm overthinking it. Anyways, help is always appreciated, thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WeaponCreator : MonoBehaviour {
public int magAmmo;
public int maxMagAmmo;
public int reserveAmmo;
public float reloadSpeed;
private float reloadTime = 10;
private int shotsFired;
void Update () {
magAmmoText.text = magAmmo.ToString();
reserveAmmoText.text = reserveAmmo.ToString();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
RELOAD STUFF BELOW
///////////////////////////////////////////////////////////////////////////////////////
if(magAmmo < maxMagAmmo && Input.GetKeyDown(KeyCode.R))
{
Reload();
}
if (magAmmo <= 0)
{
Reload();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
if (Input.GetKey(KeyCode.Mouse0) && Time.time > nextFire)
{
if (magAmmo > 0)
{
Shot();
shotSound.Play();
magAmmo--;
shotsFired++;
muzzleFlash.Play();
}
}
}
void Shot()
{
nextFire = Time.time + fireRate;
RaycastHit hit;
if (Physics.Raycast(shotPoint.position, shotPoint.forward, out hit, range))
{
if (hit.collider.tag == "Dirt")
{
Instantiate(dirt, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
}
if (hit.collider.tag == "Metal")
{
Instantiate(metal, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
}
if (hit.collider.tag == "Sand")
{
Instantiate(sand, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
}
if (hit.collider.tag == "Wood")
{
Instantiate(wood, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
}
if (hit.collider.tag == "Water")
{
Instantiate(water, hit.point, Quaternion.identity);
}
if (hit.collider.tag == "Enemy")
{
Instantiate(enemy, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
RELOAD FUNCTION
///////////////////////////////////////////////////////////////////////////////////////////////////
void Reload()
{
reloadTime -= Time.deltaTime * reloadSpeed;
if(reloadTime <= 0)
{
magAmmo = maxMagAmmo;
reserveAmmo -= shotsFired;
reloadTime = 10;
shotsFired = 0;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Answer by MT369MT · Jun 03, 2018 at 06:59 AM
Hi, I see you used GetKeyDown that works only one time when you click the key and it will also call the Reload function only one time. Use GetKey instead.
if(magAmmo < maxMagAmmo && Input.GetKey(KeyCode.R))
{
Reload();
}
Remembre that if you want to reload your ammo you must hold the button. If you want to click the key only once you must add for example a bool variable that check if you clicked your key.
if(magAmmo < maxMagAmmo && Input.GetKey(KeyCode.R))
{
CanReload = true;
}
if (magAmmo <= 0 || CanReload == true)
{
Reload();
}