Fire Rate Not Changing (C#)
So I have been making a top-down shooter style game, and it has been going pretty well, other than the fact that when I change the value of "timeBetweenShots" (the fire rate variable) it doesn't change the fire rate at all, no matter if I set it at 0.001, 0.1, 1, or even 100. It stays a constant line of bullets. I have tried changing around the script a bit, but nothing changes. Please look at my script, and tell me what is wrong if you can.
using UnityEngine;
using System.Collections;
public class GunFire : MonoBehaviour {
public bool isFiring;
public BulletScript bullet;
public float bulletSpeed;
public float timeBetweenShots;
private float shotCounter;
public Transform firePoint;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(isFiring)
{
shotCounter -= Time.deltaTime;
if (shotCounter <= 0)
{
shotCounter = timeBetweenShots;
BulletScript newBullet = Instantiate (bullet, firePoint.position, firePoint.rotation) as BulletScript;
newBullet.speed = bulletSpeed;
}
else {
shotCounter = 0;
}
}
}
}
Your if statement is executed if shotCounter is less or equal to zero, but if not you setting it to zero in the else statement. So the update after you set it to timeBetweenShots it will be equal zero again.
Answer by aditya · Nov 16, 2016 at 05:33 AM
From If Else of shotCounter just remove the else block because it will never let the If block to execute
Accept if it did the trick