Reload only when not firing
So I'm having an issue where my character can reload while he's still firing his gun. I tried implementing boolean values so that the script checks whether or not the player is firing their gun at the time they press the 'R' key,
However, I haven't gotten it quite there yet. Could someone possibly please offer an explanation as to how I can get my character to reload ONLY when they are not firing?
void Update ()
{
timer += Time.deltaTime;
checkAmmo (); //Check if player has ammo
if (Input.GetKey (KeyCode.Mouse0) && timer >= timeBetweenBullets && hasAmmo) { //If the player has ammo
Shoot (); //The player can shoot (refer to Shoot() function below)
}
else
{
isShooting = false;
}
if (timer >= timeBetweenBullets * effectsDisplayTime)
{
DisableEffects();
}
//When the mouse button isn't held, then the player isn't shooting and thus shooting is false
countAmmo ();
Reload ();
}
public void DisableEffects()
{
//gunLine.enabled() = false;
gunLight.enabled = false;
}
void Shoot()
{
isShooting = true;
timer = 0f;
gunAudio.clip = gunshotClip;
gunAudio.Play();
gunLight.enabled = true;
gunParticles.Stop();
gunParticles.Play();
//gunLine.enabled = true;
//gunLine.SetPosition(0, transform.forward);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
if (Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent<EnemyHealth>();
if(enemyHealth != null)
{
enemyHealth.TakeDamage(damagePerShot, shootHit.point);
}
//gunLine.SetPosition(1, shootHit.point);
}
else
{
//gunLine.SetPosition(1, shootRay.origin + shootRay.direction *range);
}
ammoCount -= 1;
}
void checkAmmo()
{
if (ammoCount > 0)
{
hasAmmo = true;
}
else
{
hasAmmo = false;
reloadPromptText.text = "PRESS 'R' TO RELOAD";
if(Input.GetKeyDown (KeyCode.Mouse0) && hasAmmo == false)
{
gunAudio.clip = noAmmoClip;
gunAudio.Play ();
}
}
}
void Reload()
{
if (Input.GetKeyDown (KeyCode.R) && isShooting == false) //If the player has pressed the reload 'R' button AND isn't shooting
{
ammoCount = maxAmmoCount; //Reload!
gunAudio.clip = reloadClip;
gunAudio.Play ();
reloadPromptText.text = "";
}
}
void countAmmo()
{
ammoText.text = ammoCount + "/" + maxAmmoCount;
}
}
Answer by DoctorChar · Apr 16, 2017 at 10:04 PM
Make Reloading an else to shooting so they cant both happen (keep the isreloading == false in the if statement, though).
Your answer
Follow this Question
Related Questions
How do i add a reload icon for my guns? 1 Answer
Shoot until i release the button, but with a fire ratio 2 Answers
Help me to make a delay when shooting 0 Answers
What is wrong with my code? (Weapon Reload) 0 Answers
how to add shoot cooldown 1 Answer