Slowing down the speed of my bullets whilst holding in the key.

Hey guys.

I'm quite the n00b in Unity but im slowly getting more comfortable with it. I've since a few hours ago, run into a small problem, well 2 problems actually, but they are both practically identical, so the answers from this should help me fix the 2nd problem.

Problem:

when I use (Input.GetKey("z")) to fire my weapon, my bullets fire out like crazy, I understand this to be because the bullets fire with each update call, which is every frame. However I'm trying to find a way to have the bullets STOP firing for a second before firing again, so its not as much as a constant stream of bullets.

My code, is as follows, this is an expansion of the 3DBUZZ video series, so that code is used as my base.

Blockquote using UnityEngine; using System.Collections;

public class Player : MonoBehaviour {

// Use this for initialization

public float PlayerSpeed;
public GameObject ProjectilePrefab;

// Update is called once per frame
void Update () 
{
    float amountToMoveHorizontal = Input.GetAxisRaw("Horizontal") * PlayerSpeed * Time.deltaTime;
    transform.Translate(Vector3.right * amountToMoveHorizontal);

    float amountToMoveVertical = Input.GetAxisRaw("Vertical") * PlayerSpeed * Time.deltaTime;
    transform.Translate(Vector3.up * amountToMoveVertical);

    //if (Input.GetKey("z"))
    if (Input.GetKey("z"))
    {
        //Fire Projectile on the Left & Right (Note...Eventually make into a single command...)

        Vector3 leftposition = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
        Instantiate(ProjectilePrefab, leftposition, transform.rotation);

        Vector3 rightposition = new Vector3(transform.position.x + transform.localScale.x * 8, transform.position.y + transform.localScale.y * 5);
        Instantiate(ProjectilePrefab, rightposition, transform.rotation);

        Vector3 right2position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
        Instantiate(ProjectilePrefab, right2position, Quaternion.Euler(0, 0, 20));

        Vector3 right3position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
        Instantiate(ProjectilePrefab, right3position, Quaternion.Euler(0, 0, 40));

        Vector3 right4position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
        Instantiate(ProjectilePrefab, right4position, Quaternion.Euler(0, 0, 60));

        Vector3 right5position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
        Instantiate(ProjectilePrefab, right5position, Quaternion.Euler(0, 0, 70));

        Vector3 right6position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
        Instantiate(ProjectilePrefab, right6position, Quaternion.Euler(0, 0, 80));

        Vector3 right7position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
        Instantiate(ProjectilePrefab, right7position, Quaternion.Euler(0, 0, 85));

        Vector3 right8position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
        Instantiate(ProjectilePrefab, right8position, Quaternion.Euler(0, 0, 90));

    }

}

} > Blockquote

The easiest way to do this is to keep track of when the last bullet was fired:

float firingRate = 0.1f; //delay between shots, in seconds
float lastFired = -100f; //absolute time of last fired shot

void Update() {
    //early out if we have fired recently
    if (Time.time < lastFired + firingRate) {
        return;
    }

    //if we can fire, we record the new lastFired time
    lastFired = Time.time;

    //and actually fire the bullet
    //... firing code here
}