Rate of Fire on a Gun.

var projectile : Rigidbody;
var speed = 20;

function Update () {

if ( Input.GetButton ("Fire1")) {

clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));

Destroy (clone.gameObject, 3);

}}

I got this script attached to a gun’s barrel, with a bullet being a child of the barrel to make it shoot. But what do i need to add to make the gun’s rate of fire change?

Would i need to have something like this:

yield WaitForSeconds ( *# of seconds )?

var projectile : Rigidbody;
var speed = 20;
var loaded = false;
var RPS = 5.0;

function Update () {

if ( Input.GetButton ("Fire1")  loaded) {

clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));

Destroy (clone.gameObject, 3);

Reload();

}}

function Reload(){
loaded = false;
yield WaitForSeconds(1/RPS);
loaded=true;
}

Alright, ty bro!

Btw, you seem very good at scripting, im a noob at it :stuck_out_tongue:

Couple years of practice + answering this exact same question a fair few times :slight_smile:

I haven’t tested it - so all the syntax is completely off of memory.

It worked flawlessly, thanks!

Now i need to recruit a scripter to make a script that gives a gun a limited amount of ammo before it stops and needs to be reloaded :stuck_out_tongue:

I found a level designer / modeler on the BF3 forums :slight_smile:

var projectile: Rigidbody;
var speed = 20;
var loaded = false;
var RPS = 5.0;
var clipCapacity = 30;
var clipCount = 30;
var reloadTime = 2;


function Update()
{

    if (Input.GetButton("Fire1")  loaded)
    {

        clone = Instantiate(projectile, transform.position, transform.rotation);
        clone.velocity = transform.TransformDirection(Vector3(0, 0, speed));

        Destroy(clone.gameObject, 3);

        Load();

    }
}

function Load()
{
    loaded = false;
    clipCount--;
    if (clipCount < 1)
    { //AutoReload
        yield WaitForSeconds(reloadTime);
        clipCount = clipCapacity;
    }
    else
    {  //proceed as normal
        yield WaitForSeconds(1 / RPS);
    }
    loaded = true;
}

If i had any money i would pay you! But you know how it is, Obama and all…

Wow! Flawless script, thank you so much!

How could i edit this to make it have to play a reload animation to shoot again after the magazine has been emptied?

I’d probably use one of these:

e.g.

function Load()
{
    loaded = false;
    clipCount--;
    if (clipCount < 1)
    { //AutoReload
       animation.Play("reload");  //whatever clip you are using
        yield WaitForSeconds(reloadTime);
        clipCount = clipCapacity;
    }
    else
    {  //proceed as normal
        yield WaitForSeconds(1 / RPS);
    }
    loaded = true;
}

But i’ve never used animations personally :frowning:

Ok, thank you! I could use someone of your knowledge on my team :slight_smile: