- Home /
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?
Answer by PaxNemesis · Dec 02, 2011 at 05:37 PM
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 :)
Is this java or C# because in java I have the error "expecting semicolon at end"?
This is C#, and these functions have to be inside a class to work :)
This all works as it should, but is there any way to stop the gun from shooting once the ammo gets to 0?
Just remove the Reload call in the Shoot function, then you manually have to reload ^^
Your answer
Follow this Question
Related Questions
Wait for reload to finish? 1 Answer
Reloading Help 2 Answers
Third Person Help 4 Answers
Animation Scripting Help 2 Answers
Character controller problem? 2 Answers