AI Shooting Script

Hey guys, I’m making a 2D game for a university project and the only trouble that I’m having is coming up with a script which delays the shooting of the AI, as it constantly shoots at one speed and it is way too fast.

here’s the script i’m using…

var rocket : Rigidbody;
var speed = 400.0;
var reloadTime = 0.2;
var target : Transform;
var fireRate = .5;

function Start () {

	target = GameObject.FindWithTag("Turret").transform;
	

		}
function Update () {
BombsAway ();

 }
			
function BombsAway () {
	var rocketClone : Rigidbody = Instantiate (rocket, transform.position, transform.rotation);
	rocketClone.velocity = transform.forward * speed;
	}

Thanks in advance!

var fireRate = 0.4;
var lastShot = -10.0;

function Update(){
    if(Time.time > fireRate + lastShot){
       BombsAway(); 
    lastShot = Time.time;
    }
}

Kinda simple, but very effective.

You are launching a rocket each frame which is probably a lot too fast, try adding this under the function BombsAway:

yield WaitForSeconds(fireRate);

This should wait for the time set by fireRate (in your script set to 0.5 atm) before shooting a second rocket…
Make sure to add this lign after rocketClone.velocity = …etc!
so:

function BombsAway () {
    var rocketClone : Rigidbody = Instantiate (rocket, transform.position, transform.rotation);
    rocketClone.velocity = transform.forward * speed;
    yield WaitForSeconds(fireRate);
    }

This should work but tell me if there are problems, thanks