Question by
KaoticTherium · Mar 19, 2019 at 01:17 AM ·
c#unity 5weapon systemcooldownfire rate
Weapon Fire Rate does not change intervals between each bullet C#
I'm currently making a top down shooter and I ran into a problem where when I fire my weapon, it shoots all the bullets really fast and when I change my fire rate it does not change the intervals between each created bullet.
Here is my script for my gun;
public class Gun : MonoBehaviour
{
public bool shooting;
public Bullet projectile;
public float projecticlespeed;
public float fireRate;
private float timeTillNextShot;
private bool reloading;
public int maxAmmo = 30;
private int currentAmmo;
public float reloadTime = 1f;
public Transform ExitPoint;
// Start is called before the first frame update
void Start()
{
currentAmmo = maxAmmo;
}
// Update is called once per frame
void Update()
{
isFiring();
if (shooting == true && reloading == false)
{
timeTillNextShot -= Time.deltaTime;
if (timeTillNextShot <= 0)
{
timeTillNextShot = fireRate;
if (currentAmmo == 0)
{
reloading = true;
}
Bullet newBullet = Instantiate(projectile, ExitPoint.position, ExitPoint.rotation) as Bullet;
newBullet.moveSpeed = projecticlespeed;
currentAmmo = currentAmmo - 1;
}
else
{
timeTillNextShot = 0;
}
}
}
void isFiring()
{
if (Input.GetMouseButtonDown(0)&& !reloading)
{
shooting = true;
}
if (Input.GetMouseButtonUp(0))
{
shooting = false;
}
if (Input.GetKeyDown(KeyCode.R))
{
StartCoroutine(reload());
return;
}
}
IEnumerator reload()
{
Debug.Log("Reloading in progress");
yield return new WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
reloading = false;
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Canvases visual overlap issue 2 Answers
Unexpected Symbol 'break' 2 Answers
Problem with Score/Highscore script 0 Answers
Player hiding inside Gameobject 1 Answer