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?
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