How to add a delay to my script.

Okay so recently i asked about my gun that’s bugging out and i got one awesner and it said:

Just glancing over it, it looks like:

yield WaitForSeconds(FireRate);

inside your fire function could be giving you some trouble! It doesnt count down ammo before after the fire rate. But I would try putting the delay somewhere else, so that you can’t shoot another bullet until the firerate is done.

Here is my code:

var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
var ReloadTime : float = 2;
var AmmoInMag : float = 30;
var IsFullAuto = true;
static var AmmoLeft : float;
static var ReloadTTime : float;
private var CanFire = true;
var FireRate = 0.1;
static var IsReloading = false;


function Start () {
    AmmoLeft = AmmoInMag;
    ReloadTTime = ReloadTime;
}

function Update () {
    if(IsFullAuto == false) {
       if(Input.GetButtonDown("Fire1")){
         if(AmmoLeft > 0) {
          BroadcastMessage("FireAnim");
          Fire();
         }
       }
    }
    else{
       if(Input.GetButton("Fire1")){
         if(AmmoLeft > 0) {
          BroadcastMessage("FireAnim");
          Fire();
          }
         }
       }

       if(AmmoLeft == 0)
       {
         Reload();
       }

       if(AmmoLeft < 0){
         AmmoLeft = 0;
       }
    }
function Fire () {
    if(CanFire == true && IsReloading == false){     
       var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
       bullet1.AddForce(transform.forward*BulletSpeed);
       CanFire = false;
       yield WaitForSeconds(FireRate);
       CanFire = true;
       AmmoLeft -= 1;
       audio.Play();
    }   
}

function Reload () {
    CanFire = false;
    IsReloading = true;
    BroadcastMessage("ReloadAnim");
    yield WaitForSeconds(ReloadTime);
    IsReloading = false;
    CanFire = true;
    AmmoLeft = AmmoInMag;


}

How do I add a delay?

Thank you.

PS here is a link to the problem post:

PPS here is a link to a YouTube video showing the problem:

Ok, well now I have the time. Saw your vid too. Very impressive for a “complete noob” ;). Well, just so you know, your fire rate is quite fast. Not saying that may be the only problem (or even one), but its possible.If it shoots once per frame, then it usually shoots faster than that… Depending on your frame rate.

Well, try this

//Set this to Time.time when you fire.
var fireTimerStart : float;

function Update () {
	//other stuff....
	
	if(Time.time >= fireTimerStart + fireRate) {
		canFire = true;
	}
}

If you set fireTimerStart right after you shoot, it should work, in theory. I say that because it wasn’t tested. Just wrote it on the fly :P. Hope it works though.

Also some general notes about scripting:

  • Usually you declare variable with the first letter lowercase. This is called camel hump I think… eg. bulletSpeed. This makes it look nicer in unity.
  • When using if statements with booleans, you can just say if(canfire), instead of if(canfire == true). For false, you can do if(!canfire). This is very useful to check to see if something is null. Like say a variable “thingamajig” is of type GameObject and it is not assigned, you can check that by saying if(thingamajig) or if(!thingamajig) very useful to know.

I hope the timer is what you are looking for. But before you try it, check to see what happens when you increase the fire rate, to say 2.

I hope you find the answer! :smiley:

EDIT:

I had to reverse engineer your script that you posted in the comments.

var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
var ReloadTime : float = 2;
var AmmoInMag : float = 30;
var IsFullAuto = true;
static var AmmoLeft : float;
static var ReloadTTime : float;
private var CanFire = true;
var FireRate = 0.1;
static var IsReloading = false;
var fireTimerStart : float;

function Start ()
{
	AmmoLeft = AmmoInMag; ReloadTTime = ReloadTime;
}
function Update ()
{
	if(Time.time >= fireTimerStart + FireRate)
	{ 
		canFire = true; 
	}
	if(IsFullAuto == false) 
	{
		if(Input.GetButtonDown("Fire1"))
		{ 
			if(AmmoLeft > 0)
			{ 
				BroadcastMessage("FireAnim"); 
				Fire(); 
			} 	
		} 
	}
} //Meater6: You were missing this close bracket!

//(i have cut the bottom out) And setting my FireRate to 2 dosnt help D: and what would i set the fireTimeStart to if my FireRate is at 0.1?

Ok, I think I see your problem. You never set fireStartTime. Also, you should be sure that canFire is false while the timmer is going. Set it to Time.time right after you fire, this means preferably after you instantiate the bullet or fire the raycast. But this should work too (Find this section in your script, and the line):

if(AmmoLeft > 0)
{ 
    BroadcastMessage("FireAnim"); 
	Fire();
    //NEW LINES BELOW
    fireStartTime = Time.time;
    canFire = false;
} 	

And that’s it. I mean, in theory it should work.

What you need is a timer that you reset. From a quick look all other answers do not reset the timer meaning it waits and then trigger 60 times per second. I could be wrong though I just overlooked…

var timer :float = 0;
var fireRate = 0.5f;
function Update () {
    if(Input.GetMouseButton(0)){
        timer -=Time.DeltaTime;
        if(timer <= 0) {
           Fire();
           timer = fireRate;
        }
    }
    if(Input.GetMouseButtonUp(0))timer=0.0f;
}

When you press, timer is 0 so Fire is called and timer is set to the fireRate. As you keep pressing timer is decreased but as long as it is not 0 Fire is not called. That is until obviously timer reaches 0.

Also, when you release the button, you want to be able to shoot right away next time you press as it would not make sense to have a delay. So GetMouseButtonUp set timer back to 0.

javascript Version:

Call MyWaitFunc() where you want to delay.

 function MyWaitFunc () {
        yield WaitForSeconds (5);
        print("This line will print after 2 seconds");
    }