Making a rate of fire.

I am currently working on a script for the enemy to shoot instantiated bullet prefabs at me, I have everything working well except being able to set a rate of fire on my script, i am trying to use a waitforseconds function but i dont think i am doing it properly and need pointing in the right direction.

The code is supposed to wait 5 seconds between each shot.

thanks for any help.

using UnityEngine;
using System.Collections;

public class EnemyShootingScript : MonoBehaviour {
  public bool ShootAtPlayer = false;
  public Transform barrelEnd;
  public Rigidbody Bullet;
  public float bulletSpeed;
  public Animation Recoil;
  public bool allowFire;

  // Use this for initialization
  void Start () {
   
   }
   
   // Update is called once per frame
   void Update () {
  if (ShootAtPlayer&&allowFire)
  {
  allowFire = false;
  Instantiate(Bullet, barrelEnd.position, barrelEnd.rotation);
  GetComponent<Animation>().Play("Recoil");
  GetComponent<AudioSource>().Play();
  new WaitForSeconds(5);
  allowFire = true;
  }
   
   }

  void OnTriggerEnter(Collider other)
  {
  if (other.CompareTag("Player"))
  ShootAtPlayer = true;
  }
}[code]

Try using a timer variable to keep track of how much time has passed between shots.

public float shotTimer;
public float shotPeriod;     //Assuming this is set to 5 in inspector

...

if (shotTimer = 0)     //If there is no shot cooldown...
{
     //Perform firing actions
     Instantiate(Bullet, barrelEnd.position, barrelEnd.rotation);
     GetComponent<Animation>().[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Play']Play[/URL]("Recoil");
     GetComponent<AudioSource>().[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Play']Play[/URL]();
    
     shotTimer = shotPeriod;
    
}

if (shotTimer > 0)     //If there is a shot cooldown...
{
     shotTimer -= Time.deltaTime;     //Subtract time since last frame from timer
     shotTimer = Mathf.Max(shotTimer, 0);     //If timer is less than 0, return 0
}