- Home /
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
Answer by testure · Jul 24, 2011 at 04:26 AM
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();
It does not take no effect the bullets still go in the same speed and there is no delay
Can you please use my variable name in the condition you have written :). Do i place it in the update function ?
Sorry, maybe someone else will- but I have a strict 'no writing code for people' policy. I don't $$anonymous$$d giving an example and explaining something if it's not clear, but I'm not going to write your code for you.
Your answer
Follow this Question
Related Questions
Raycast hitting below mouse position :( 0 Answers
Question about Gun Sounds 1 Answer
Fps weapon shoot/monster damage 2 Answers
Destroy gameObject on collision/time? 0 Answers