i have this script (the fire input that removes a bullet is added into this script):
static var load = 8;
var seeLoad = 8;
var ammo = 100;
var reloadTime = 3.0;
var pistol : Transform;
function Update () {
if(Input.GetButtonDown("Fire1")){
if(load >= 1){
load -= 1;
}
}
if(Input.GetButtonDown("r")){
reload();
pistol.animation.CrossFade("pistol reload");
}
if(load <= 0){
reload();
pistol.animation.CrossFade("pistol reload");
}
seeLoad = load;
}
function reload () {
yield WaitForSeconds(reloadTime);
load = 8;
}
the problem is that when the 3 seconds are over and the load var is put back to 8 for another 4 seconds if i fire it stays at 8, the waitForSeconds makes it equal to 8 for this time⦠is there a way to make it equal 8 once then be changeable?
thanks
Your code formatted (use the code tag):
static var load = 8;
var seeLoad = 8;
var ammo = 100;
var reloadTime = 3.0;
var pistol : Transform;
function Update () {
if(Input.GetButtonDown("Fire1")){
if(load >= 1){
load -= 1;
}
}
if(Input.GetButtonDown("r")){
reload();
pistol.animation.CrossFade("pistol reload");
}
if(load <= 0){ //check for reloading!
reload();
pistol.animation.CrossFade("pistol reload");
}
seeLoad = load;
}
function reload () { //add reloading = true here!
yield WaitForSeconds(reloadTime);
load = 8;
}
This seems to be the problem: you're calling reload() every frame that load is at 0, including the time that you're WaitForSeconds(reloadTime);
One solution would be to add a boolean called "reloading", and make the load check:
if(load <= 0 && !reloading){
reload();
pistol.animation.CrossFade("pistol reload");
}
Set reloading to true at the start of your reload() function, this way it won't call reload if it has already, and add a condition in update to set it back to false like so:
if(load > 0) reloading = false
Or just put reloading = false after you set load to 8, so you can reload more than once ever :D