x


Reload Tutorial?

How would I go about writing a reload sort of script, are there any tutorials on this or could someone explain it to me? I already have a reload animation which is triggered when r is pressed but apart from that I dont know what to do. I need some sort of script which would allow my gun to shoot say 8 times before it has to reload. and then some sort of script which would stop the player from being able to shoot, whilst it reloads. I realize this is all very complex but can anyone help me out?

more ▼

asked Dec 01 '11 at 09:31 PM

Dreave gravatar image

Dreave
122 82 94 96

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

I don't have any tutorial for you, but you might want to try something like this: C#

    int count = 8;
    bool reloading = false;

    void Update()
    {
       if (Input.GetKeyDown(KeyCode.R))
         Reload();

       // Should only be able to shoot if we aren't reloading and still have ammo.
       if (!reloading && count > 0 && Input.GetButtonDown("Fire1"))
         Shoot();
    }

    void Shoot()
    {
       // Do shooting stuff.
       --count;

       // If we used our last shot, then we should reload.
       if (count <= 0)
         Reload();
    }

    void Reload()
    {
       reloading = true;

       // Do reloading stuff.
       count = 8;

       reloading = false;
    }

JavaScript Version:

var count : int = 8;
var reloading : boolean = false;

function Update()
{
    if (Input.GetKeyDown(KeyCode.R))
        Reload();

    // Should only be able to shoot if we aren't reloading and still have ammo.
    if (!reloading && count > 0 && Input.GetButtonDown("Fire1"))
        Shoot();
}

function Shoot()
{
    // Do shooting stuff.
    count--;

    // If we used our last shot, then we should reload.
    if (count <= 0)
        Reload();
}

function Reload()
{
    reloading = true;

    // Do reloading stuff.
    count = 8;

    reloading = false;
}

Hope it helps :)

more ▼

answered Dec 02 '11 at 05:37 PM

PaxNemesis gravatar image

PaxNemesis
235 9 11 21

Is this java or C# because in java I have the error "expecting semicolon at end"?

Dec 02 '11 at 06:41 PM Dreave

This is C#, and these functions have to be inside a class to work :)

Dec 02 '11 at 06:45 PM PaxNemesis

Added the javascript version :)

Dec 02 '11 at 07:05 PM PaxNemesis

This all works as it should, but is there any way to stop the gun from shooting once the ammo gets to 0?

Dec 02 '11 at 07:25 PM Dreave

Just remove the Reload call in the Shoot function, then you manually have to reload ^^

Dec 02 '11 at 07:36 PM PaxNemesis
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5081
x3791
x3738
x85

asked: Dec 01 '11 at 09:31 PM

Seen: 707 times

Last Updated: Dec 02 '11 at 09:20 PM