How can i get an enemy ai to shoot a prefab only once every 3 seconds after i come into its range?

using UnityEngine;
using System.Collections;

public class EnemyAi : MonoBehaviour {
	public GameObject Player;
	public float Range = 40;
	public Rigidbody rocketPrefab;
	public Transform barrelEnd;
	public float shotInterval = 3;

	void Update () {
		if(CircleCircle(Player.transform.position,1,transform.position,Range)) {

			transform.LookAt(Player.transform );


	public bool CircleCircle(Vector3 P1, float R1, Vector3 P2, float R2) {

	void OnDrawGizmos() {
		Gizmos.color = Color.white;
		Gizmos.DrawWireSphere(transform.position, Range);

	IEnumerator Tick() {
		while(true) {
			Rigidbody rocketInstance;
			rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
			rocketInstance.AddForce(barrelEnd.forward * 5000);

			yield return new WaitForSeconds(5f);


public float shootInterval = 3f;
private float timeSinceLastShot = 0f;

void Update(){
    timeSinceLastShot += Time.deltaTime();

void shoot(){
    if(timeSinceLastShot < shootInterval){
        return; //3 seconds have not passed yet, do not shoot.
    //we can shoot.
    //do damage or whatever here
    timeSinceLastShot = 0;

This is just example code. Look at it and modify your code. Copy-pasting this code will not work.

Instead of having this logic in the update code, which runs every frame, you could use 2 coroutines.

One checks if the prefab is in range, could run every 0.5 seconds. If something is in range, then a inRange flag is set.

The other runs every 3 seconds, and if the inRange flag is set, triggers a shot.

This lets you tune how often you do the checks, and allows you to build other behavior relying on the isRange flag (like turning turrets etc).