- Home /
Yield WaitForSeconds only works sometimes?!
Ok so I'm fairly new to the Unity community and I'm trying to begin some sort of FPS, but something is just not working. After my reload animation, if I shoot (full auto) any time within like 2 seconds of when the reload animation ended, I can shoot millions of bullets out rapidly as if the WaitForSeconds as decided to not work. If I wait more than 4 seconds after reload, it works just fine with an even automatic fire. Help?!
Heres my script:
var Bullet : Rigidbody;
var BulletSpeed : float = 1000;
var ReloadTime : float = 2.2;
var Ammo : float = 30;
var Spawn : Transform;
var IsFullAuto = true;
@script HideInInspector
static var ReloadTTime : float;
static var IsReloading = false;
static var AmmoLeft : float;
private var CanFire = true;
function Start () {
AmmoLeft = Ammo;
ReloadTTime = ReloadTime;
}
function Update () {
if(Input.GetButton("Fire1")){
if(AmmoLeft > 0){
if(CanFire == true && IsReloading == false){
BroadcastMessage("FireAnim");
Fire();
}
}
}
if(AmmoLeft == 0)
{
BroadcastMessage("ReloadAnim");
Reload();
}
if(AmmoLeft < 0){
AmmoLeft = 0;
}
}
function Fire(){
var FireRate = 0.2;
CanFire = false;
yield WaitForSeconds(FireRate);
CanFire = true;
var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
bullet1.AddForce(Spawn.forward * BulletSpeed);
AmmoLeft -= 1;
audio.Play();
}
function Reload(){
CanFire = false;
IsReloading = true;
yield WaitForSeconds(ReloadTime);
AmmoLeft = Ammo;
IsReloading = false;
CanFire = true;
}
yield will stop the execution of the coroutine, continuing its execution after the yield instruction, the next frame or your specified wait time.
I'm guessing you know this but you are missing the consequences.
Rethink your logic as it appears that your flags are beco$$anonymous$$g set in an undesirable way as a result.
I feel like WaitForSeconds is to be avoided unless absolutely necessary, especially in situations like this. Not a proper answer to your question as it doesn't point out the flaws in the code logic, but i recently answered this which might help you rewrite it.
Answer by Taxen0 · Dec 23, 2014 at 10:40 AM
Try the following changes, but as other have stated I too usually try and stay away from yield. in this case you could instead have a variable that stores the last time you fired and then compare it to the current time and see if the defference is larger than the firerate. But both ways should work fine =)
I usually code in c# so if i missed anything read more about this here
function Update () {
if(Input.GetButton("Fire1")){
if(AmmoLeft > 0){
if(CanFire == true && IsReloading == false){
BroadcastMessage("FireAnim");
yield StartCoroutine(Fire());
}
}
}
}
function Fire(){
var FireRate = 0.2;
CanFire = false;
var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
bullet1.AddForce(Spawn.forward * BulletSpeed);
AmmoLeft -= 1;
audio.Play();
yield WaitForSeconds(FireRate);
CanFire = true;
}
Your answer
Follow this Question
Related Questions
Waiting between pingpong loops 2 Answers
WaitForSeconds not working C# 2 Answers
What is wrong with this use of WaitForSeconds? 1 Answer