x


How Can i add a delay to my shoot script ?

Hi all i just made a shooting script and it works perfectly :) but the problem is the shooting is to fast i want a delay in there so if i press F on the keyboard rapidly it wont let me shoot until the delay time is over. I want to set the delay time as 1.5 sec and then the you can shoot again. After 1.5 sec you can shoot :) when you press F on the keyboard

Here is my script:

       var Range : float;

          var Force : float = 1000;

              var Clips : float = 100;

                 var BulletsPerClip : float = 1;

                 var ReloadTime: float = 0.6;


 var BulletsLeft : float = 0;


 var Delay : float = 2;
var fireRate = 0.05;

public var ShootSound : AudioClip;
public var ReloadSound : AudioClip;
function Update () {

       if(Input.GetKeyDown(KeyCode.F) && BulletsLeft ){

         PlayShootSound();
          ShootRay();



}

}

function Start(){


   BulletsLeft = BulletsPerClip;



}




function ShootRay(){

  GameObject.Find("m1911pistoleReloadandShooting").animation.Play("Shoot") ;
    var hit: RaycastHit;

    var direction = transform.TransformDirection(Vector3.forward);

    Debug.DrawRay(transform.position, direction * Range, Color.yellow);

    if(Physics.Raycast( transform.position, direction, hit, Range)){

        if(hit.rigidbody){


         hit.rigidbody.AddForceAtPosition( Force * direction , hit.point);

    }
    }

BulletsLeft--;

if( BulletsLeft < 0){

     BulletsLeft = 0;
 }    

if( BulletsLeft == 0){

 Reload();

}
}


function Reload (){

PlayReloadSound();
GameObject.Find("m1911pistoleReloadandShooting").animation.Play("Reload") ;
 yield WaitForSeconds( ReloadTime);

   if( Clips >0){

  Clips -=1;
  BulletsLeft = BulletsPerClip; 

 }
}

    function PlayShootSound()
    {
       audio.PlayOneShot(ShootSound);
    }

function PlayReloadSound()
    {
       audio.PlayOneShot(ReloadSound);
    }

Please someone help me thank you so much :) MCHALO

more ▼

asked Jul 24 '11 at 04:19 AM

MC HALO gravatar image

MC HALO
878 74 94 111

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

use invoke to delay calling something. If you want to keep people from being able to mash on buttons as fast as they can, you can just do a conditional time test. IE)

if(Time.time - startTime >= minShotTime)    
    ShootGun();
more ▼

answered Jul 24 '11 at 04:26 AM

testure gravatar image

testure
4.2k 20 25 48

It does not take no effect the bullets still go in the same speed and there is no delay

Jul 24 '11 at 04:45 AM MC HALO

Can you please use my variable name in the condition you have written :). Do i place it in the update function ?

Jul 24 '11 at 04:46 AM MC HALO

Sorry, maybe someone else will- but I have a strict 'no writing code for people' policy. I don't mind giving an example and explaining something if it's not clear, but I'm not going to write your code for you.

Jul 24 '11 at 04:48 AM testure

dont worry about it mate im sure someone will

Jul 24 '11 at 04:54 AM MC HALO
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1172
x329

asked: Jul 24 '11 at 04:19 AM

Seen: 754 times

Last Updated: Jul 24 '11 at 04:54 AM