- Home /
Shooting and reloading mechanism problem.
Maybe at first I should show my script and explain the problem. Okay, so the problem is shooting like 100 times per second. Not literally but It's like super rapid fire gun. When I shoot, the current ammo (30/120, 29/120, 28/120 and so on) goes down till It's -99 or so and then immediately goes back to about -10, slows down and starts to reduce again to -99 and it goes like that forever. The total ammo which is in this case 120 is always the same. The reload coroutine works but I can shoot while reloading and then the ammo resets to 30 here but it does not stop going down and down.
The script looks like this:
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
StartCoroutine(Reload());
}
}
IEnumerator Reload()
{
canShoot = false;
yield return new WaitForSeconds(2);
gameManager.GetComponent<GameManager>().AmmoCurrent.text = ammoReload.ToString();
ammoCurrentInt = ammoReload;
canShoot = true;
}
public void Shoot()
{
if (isShooting && ammoCurrentInt == 0 && canShoot == false)
return;
isShooting = true;
GameObject flash = Instantiate(FlashEffect, Vector3.zero, Quaternion.identity);
flash.transform.SetParent(BulletEmiter);
flash.transform.localPosition = Vector3.zero;
flash.transform.localRotation = BulletEmiter.localRotation;
ammoCurrentInt--;
gameManager.GetComponent<GameManager>().AmmoCurrent.text = ammoCurrentInt.ToString();
SoundSource.PlayOneShot(ShootSound);
Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * 200f, Color.red, 2f);
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 200f))
{
HitDetected(hit);
}
StartCoroutine(AutoFire());
}
IEnumerator AutoFire()
{
yield return new WaitForSeconds(shootDelay);
isShooting = false;
}
Okay, maybe now I should describe some of my variables as they can be confusing.
ammoReload is int, It's used to reload the gun and It's never changed. It's set to ammoCurrentInt which is the variable to display ammo which is left on the screen.
When you reload you can't shoot that is why I made canShoot variable. The coroutine AutoFire() is to make delays between shots.
If It's understable then sorry for this quite long explanation. I just made it because I wouldn't understand it if someone showed me this lol.
In the PlayerController script, attached to player:
if (Input.Get$$anonymous$$ouseButton(0))
{
playerShooter.Shoot();
}
I feel like this
if (isShooting && ammoCurrentInt == 0 && canShoot == false)
Should be this
if (ammoCurrentInt == 0 || canShoot == false)
If it isn't... then as long as you have ammo you can ALWAYS shoot. Even while reloading, even if you don't have ammo or can shoot. This is a bad line here. I would try what I wrote and see what kind of changes you get.
Answer by berndunity · Dec 11, 2018 at 07:43 PM
isShooting = true;
GameObject flash = Instantiate(FlashEffect, Vector3.zero, Quaternion.identity);
flash.transform.SetParent(BulletEmiter);
flash.transform.localPosition = Vector3.zero;
flash.transform.localRotation = BulletEmiter.localRotation;
You're never asking if canShoot is true, so this part is always called when the Shoot() method is called. I would change it to:
if (isShooting == false && ammoCurrentInt >= 1 && canShoot){
//Now you can shoot.
}
I'm asking for it here, at the beginning of the Shoot() function, if It's false, you can't shoot and if It's true, you can:
public void Shoot()
{
if (isShooting && ammoCurrentInt == 0 && canShoot == false)
return;
if (isShooting && ammoCurrentInt == 0 && canShoot == false)
return;
This part should be changed to
if(isShooting || ammoCurrentInt <= 0 || canShoot == false)
return;
Because you don't want to shoot if isShooting is true or if the ammo is = or lower then 0 or if canShoot is false, because you're reloading.
Unfortunately, still the same. It didn't fix the problem.
EDIT: wait I forgot to change the "&&" symbols. I will try now. EDIT 2: It worked! Thanks, simple mistake but couldn't figure it out.
It can't be that because, yes you could have no ammo, but canshoot could still be true, thus it still tries to shoot without ammo.
if (ammoCurrentInt == 0 || canShoot == false)
Should be this I think. I don't like less than here for ammo count because if it does get to less than zero, I want to know about it. That means a bug.
Nice well done! Can you please vote up the answer I need points ;).
Your answer
Follow this Question
Related Questions
How to shoot a bullet to the mouse cursor position? 1 Answer
shooting problem with raycasting 1 Answer
Shoot Script Help 1 Answer
Problem with firing laser in direction of camera 1 Answer
FPS rigidbody bullets not moving to center of screen 0 Answers