- Home /
Reload manually in raycast?
Hello! How can I make the gun reload by pressing the R key? since it recharges only when the bullets are equal to 0 and I do not want to get that. Thank you!
public class Weapon : MonoBehaviour {
public float Damage = 0f;
public float Range = 0f;
public float ImpactForce = 0f;
public float FireRate = 0f;
public int MaxAmmo = 0;
public int TotalAmmo = 0;
public int CurrentAmmo;
public float ReloadTime = 0;
private bool IsReloading = false;
public Camera FPScam;
public ParticleEmitter MuzzleFlash;
public GameObject HitEffect;
private float NextTimeToFire = 0f;
public AudioClip ShootSound;
void Start()
{
CurrentAmmo = MaxAmmo;
}
void OnEnable()
{
IsReloading = false;
}
void Update () {
if(IsReloading)
return;
if(CurrentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if(Input.GetButton("Fire1") && Time.time >= NextTimeToFire)
{
NextTimeToFire = Time.time + 1f/FireRate;
Shoot();
}
}
IEnumerator Reload()
{
IsReloading = true;
Debug.Log("Reloading...");
yield return new WaitForSeconds(ReloadTime - 0.25f);
TotalAmmo -= MaxAmmo;
CurrentAmmo = MaxAmmo;
IsReloading = false;
}
void Shoot()
{
MuzzleFlash.Emit();
audio.Play();
audio.clip = ShootSound;
CurrentAmmo--;
RaycastHit hit;
if (Physics.Raycast(FPScam.transform.position, FPScam.transform.forward, out hit, Range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if(target != null)
{
target.TakeDamage(Damage);
}
if(hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * ImpactForce);
}
Instantiate(HitEffect, hit.point, Quaternion.LookRotation(hit.normal));
}
}
}
Answer by kipperorrell8 · Mar 21, 2020 at 12:43 AM
Hello I’ve seen you haven’t had a reply so I’ll try and help, you might need to double check any spelling but I’ll try my best... So to check Input you want to 1. Go Edit/ProjectSettings/Input 2. Add or change on the fields and name the field Reload and give it a positive key as your saying r. 3.Close project settings. So now we’ve set up a custom button with its own name we can easily speak to this button, Just like your talking to Fire1 which is setup to mouse0. Now for the purpose of answering your question. We can add an if statement just like you have before but slightly different. And it should look something along the lines of.
if(Input.GetButton("Reload")){
//Call the Reload Method...
}
Before we call the if we can add the reload time check if that’s really what you want. And you can also check to see if the user has actually shot a bullet. And this brings us to your second question why it’s only reloading when Ammo is equal to 0 and this is because of this if statement. And because less than and equal too will only activate when current ammo is equal to or less than zero which is why reload is only activating when current ammo is equal to zero; or less. You really want to be checking that CurrentAmmo is less than TotalAmmo but greater than zero unless you want infinite ammo. Then after this check we can start a reload method, we could actually just have the if(Input.GetButton(“Reload”)) In update call the reload method after that then at the start of the reload method you wanna perform the check on currentAmmo being less than TotalAmmo and greater than zero, then we can add our maxCapacity or magCapacity to our ammo and perform the reload.
//Try this. if(Input.GetButton(“Reload”))
{
//Ammo Checks if(CurrentAmmo > 0 && CurrentAmmo < TotalAmmo) {
}
So I hope this clears everything up. We can also add other checks for things like max ammo, and mag capacity by doing little checks. Good Luck and I hope this helps someone.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how do i make a ammo and reload system 1 Answer
Renderer on object disabled after level reload 1 Answer
C# - Ammo in Shoot Array?! 1 Answer