My gun reloading dont work,can i please get help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shoot : MonoBehaviour {
public Rigidbody bulletPrefab;
public Transform bulletSpawn;
public float shootingSpeed;
public int bulletAmmount;
int shootedBulletAmmount;
public int currentBulletAmmount;
bool isSpawned;
bool canShoot;
void Start()
{
canShoot = true;
}
void Update()
{
Shoot ();
Reload ();
}
void Shoot ()
{
if(canShoot==true)
{
if (Input.GetButtonDown ("Fire1")) {
shootedBulletAmmount = shootedBulletAmmount++;
isSpawned = true;
print (shootedBulletAmmount);
Rigidbody rocketInstance;
rocketInstance = Instantiate (bulletPrefab, bulletSpawn.position, bulletSpawn.rotation) as Rigidbody;
rocketInstance.AddForce (bulletSpawn.forward * shootingSpeed);
}
}
}
void Reload()
{
if(isSpawned==true)
{
currentBulletAmmount = bulletAmmount -= shootedBulletAmmount;
}
if (currentBulletAmmount <= 0)
{
canShoot = false;
}
if (canShoot == false)
{
StartCoroutine("readyShoot");
}
}
IEnumerator readyShoot()
{
yield return new WaitForSeconds (3);
canShoot = true;
bulletAmmount = 20;
shootedBulletAmmount = 0;
currentBulletAmmount = 0;
}
}
Answer by LTonon · Jan 27, 2018 at 11:10 AM
Ok, so first of all it seems like your variable "isSpawned" doesn't actually do anything, since it'll always be "true" after the first shot... You could get rid of the first if statement on the Reload function and use simply like this:
void Reload()
{
currentBulletAmmount = bulletAmmount -= shootedBulletAmmount;
if (currentBulletAmmount <= 0)
{
canShoot = false;
}
if (canShoot == false)
{
StartCoroutine("readyShoot");
}
}
And I guess that your problem is on how you are doing the reload, since calling the function on the Update() all of the time should make you reload super fast and so it would not decrease your current number of bullets (I recommend associate a button press to it or reloading only when there are no more bullets)... Right? What exactly is happening?
first of all thanks. now just dont want to count numbers and both variables are keeped on 20 what should i do
What do you mean by "don't want to count numbers"? And did you change something on your script? Try this and tell me what happens, I tried to clean up your code a little to make easier to understand. I guess that you have an error on the script because you are using "if (isSpawned == true) { currentBulletAmount = bulletAmmount -= shootedBulletAmmount; }", but since you never change "isSpawned" to false, this is always called... And right after a reload, "currentBulletAmount = 0", meaning that it'll always reload after the first time it has reloaded.
void Reload()
{
if (bulletAmount - shootedBulletAmount <= 0) {
canShoot = false;
StartCoroutine("readyShoot");
}
}
IEnumerator readyShoot()
{
yield return new WaitForSeconds(3f);
shootedBulletAmount = 0;
canShoot = true;
}
Answer by Miso-c9 · Jan 27, 2018 at 05:52 PM
oke i tried some different and it works.. btw thx for help and waste ur time, but u helpeded me to learn some
Your answer
Follow this Question
Related Questions
Need help with shooting and reload script 1 Answer
Instantiate not repeating? 1 Answer
How Can i run a method afterequal integer number counts using Ienumerator ?? 0 Answers
Help with my Arrow Fire Script 1 Answer
Gun not reloading properly & time not deducting correctly when below 10 seconds 2 Answers