How do i change the bullets"enemyattack" rate of fire?

#pragma strict
var enemyattack : Rigidbody;
var throwSpeed:float = 50;
var throwSound: AudioClip;
var target : Transform;
var myTransform : Transform;

function Awake ()
{
myTransform = transform;

}

function Start ()
{
target = GameObject.FindWithTag(“Player”).transform;
}

function Update ()
{

        var dist = Vector3.Distance(target.position, myTransform.position);
        var lookDir = target.position - myTransform.position;
            lookDir.y = 0;

if(dist<30)
{
			
	 var clone : Rigidbody;
	 audio.PlayOneShot(throwSound);
  clone = Instantiate(enemyattack, transform.position, transform.rotation);
	clone.velocity = transform.TransformDirection (Vector3.forward * 10);
				
				
}

}

1 Answer

1

It looks like this code will fire a shot each frame. Instead put it in a separate function and use InvokeRepeating().

function shoot() {
    if(dist<30) {
        var clone : Rigidbody;
        audio.PlayOneShot(throwSound);
        clone = Instantiate(enemyattack, transform.position, transform.rotation);
        clone.velocity = transform.TransformDirection (Vector3.forward * 10);
    }
}

Then you can put this in Start():

InvokeRepeating("Shoot", 0, 0.1);

This will call Shoot() ten times per second.