- Home /
 
Shoot Delay in C#
I need to make shoot delay because it shoots all bullets which i have in 1 second or less. So here is the script:
 using UnityEngine;
 using System.Collections;
 public class Shooting : MonoBehaviour {
 public Transform bullet; //ტყვიის პრეფაბი
 public GUISkin MySkin;    //გუისკინის ასარჩევი
 public Texture2D AimTexture; //მიზნის ტექსტურა
 public int BulletForce = 5000; //ტყვიის სიჩქარე
 private int CurAmmoCount = 10; //ახლანდელი ტყვიების რაოდენობის ცვლადი
 public int MaxAmmoCount = 10; //მაქსიმალური ტყვიების რაოდენობა აბოიმაში
 public int CurCatrige = 9;    //ახლანდელი აბოიმის რაოდენობის ცვლადი
 public int MaxCatrige = 9;    //მაქსიმალური აბოიმის რაოდენობა
 public AudioClip Fire; //სროლის ხმა
 public AudioClip Reload; //გადატენვის ხმა
 public int AmmoBoxWidth = 200;    //ტყვიების ბოქსის სიგრძე
 public int AmmoBoxHeight = 100; //ტყვიების ბოქსის სიგანე
 private float ReloadTimer = 0.0f; //გადატენვის დრო
 public int OffsetAimX;     //მიზნის მდებარეობა x ღერძზე
 public int OffsetAimY;     //მიზნის მდებარეობა y ღერძზე 
 public int inventoryAmmoCount = 30; //მაქსიმალური ტყვიების რაოდენობა მოთამაშისთვის
 private int RaznicaAmmo;
 // ფუნქციები
 void Start ()
 {
 }
 void Update ()
 {
 if(Input.GetMouseButton(0)&CurAmmoCount>0&ReloadTimer<=0) //თუ დაეჭირება მაუსს ისროლოს
 {
 WaitForSeconds(1);
 Shoot();
 }
 RaznicaAmmo = MaxAmmoCount-CurAmmoCount;    //ითვლის გადატენვის დროს რამდენი უნდა დაემატოს
 if(Input.GetButtonDown("Reload Weapon")&inventoryAmmoCount>0) //გადატენვის ღილაკი
 {
 if(inventoryAmmoCount>=RaznicaAmmo)
 {
 CurAmmoCount += RaznicaAmmo;     //გადატენვისას რო დაემატოს ზუსტად იმდენი ტყვია რამდენიც უკლია რომ შეივსოს
 inventoryAmmoCount -= RaznicaAmmo; // აკლებს ტყვიების რაოდენობას საერთო რაოდენობას
 }
 else
 {
 CurAmmoCount+=inventoryAmmoCount;
 inventoryAmmoCount = 0;
 }
 audio.PlayOneShot(Reload);     
 }
 }
 void Shoot()
 {
 Transform BulletInstance = (Transform) Instantiate(bullet, GameObject.Find("BulletSpawnPoint").transform.position, Quaternion.identity); //გენერირებას უკეთებს პრეფაბ "BulletSpawnPoint"
 BulletInstance.rigidbody.AddForce(transform.forward * BulletForce); //ტყვიის სიჩქარეს უმატებს "BulletForce"
 CurAmmoCount = CurAmmoCount - 1; //-1 ტყვია
 audio.PlayOneShot(Fire); //ხმა
 }
 void OnGUI()
 {
 GUI.DrawTexture(new Rect((Screen.width-AimTexture.width)/2-OffsetAimX,(Screen.height-AimTexture.height)/2-OffsetAimY,AimTexture.width,AimTexture.height), AimTexture);
 GUI.skin = MySkin;
 GUI.Box(new Rect(Screen.width/15,Screen.height-AmmoBoxHeight,AmmoBoxWidth,AmmoBoxHeight), "Ammo:"+CurAmmoCount+"/"+inventoryAmmoCount);
 }
 }
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by moonstruck · Jun 21, 2013 at 08:23 AM
You may add 2 variables:
public float bulletTimeout = 0.1f;
float lastBullet = 0f; And use them to shoot (instead of 
               WaitForSeconds): 
               float t = Time.time;
if( ( t - lastBullet ) >= bulletTimeout ) {
    Shoot();
    lastBullet = t;
} Now your bullets will appear one per 
               bulletTimeout seconds. 
              Have you replaced both WaitForSeconds and Shoot with the second code snippet? 
yes, my core right now looks like that:
 using UnityEngine;
 using System.Collections;
 public class Shooting : $$anonymous$$onoBehaviour {
 public Transform bullet; //ტყვიის პრეფაბი
 public GUISkin $$anonymous$$ySkin;    //გუისკინის ასარჩევი
 public Texture2D AimTexture; //მიზნის ტექსტურა
 public int BulletForce = 5000; //ტყვიის სიჩქარე
 private int CurAmmoCount = 10; //ახლანდელი ტყვიების რაოდენობის ცვლადი
 public int $$anonymous$$axAmmoCount = 10; //მაქსიმალური ტყვიების რაოდენობა აბოიმაში
 public int CurCatrige = 9;    //ახლანდელი აბოიმის რაოდენობის ცვლადი
 public int $$anonymous$$axCatrige = 9;    //მაქსიმალური აბოიმის რაოდენობა
 public AudioClip Fire; //სროლის ხმა
 public AudioClip Reload; //გადატენვის ხმა
 public int AmmoBoxWidth = 200;    //ტყვიების ბოქსის სიგრძე
 public int AmmoBoxHeight = 100; //ტყვიების ბოქსის სიგანე
 private float ReloadTimer = 0.0f; //გადატენვის დრო
 public int OffsetAimX;     //მიზნის მდებარეობა x ღერძზე
 public int OffsetAimY;     //მიზნის მდებარეობა y ღერძზე 
 public int inventoryAmmoCount = 30; //მაქსიმალური ტყვიების რაოდენობა მოთამაშისთვის
 private int RaznicaAmmo;
 public float bulletTimeout = 0.1f;
 float lastBullet = 0f;
 // ფუნქციები
 void Start ()
 {
 }
 void Update ()
 {
 if(Input.Get$$anonymous$$ouseButton(0)&CurAmmoCount>0&ReloadTimer<=0) //თუ დაეჭირება მაუსს ისროლოს
 {
 Shoot();
 float t = Time.time;
 if( ( t - lastBullet ) >= bulletTimeout ) { 
     Shoot();  lastBullet = t; 
 }
 }
 RaznicaAmmo = $$anonymous$$axAmmoCount-CurAmmoCount;    //ითვლის გადატენვის დროს რამდენი უნდა დაემატოს
 if(Input.GetButtonDown("Reload Weapon")&inventoryAmmoCount>0) //გადატენვის ღილაკი
 {
 if(inventoryAmmoCount>=RaznicaAmmo)
 {
 CurAmmoCount += RaznicaAmmo;     //გადატენვისას რო დაემატოს ზუსტად იმდენი ტყვია რამდენიც უკლია რომ შეივსოს
 inventoryAmmoCount -= RaznicaAmmo; // აკლებს ტყვიების რაოდენობას საერთო რაოდენობას
 }
 else
 {
 CurAmmoCount+=inventoryAmmoCount;
 inventoryAmmoCount = 0;
 }
 audio.PlayOneShot(Reload);     
 }
 }
 void Shoot()
 {
 Transform BulletInstance = (Transform) Instantiate(bullet, GameObject.Find("BulletSpawnPoint").transform.position, Quaternion.identity); //გენერირებას უკეთებს პრეფაბ "BulletSpawnPoint"
 BulletInstance.rigidbody.AddForce(transform.forward * BulletForce); //ტყვიის სიჩქარეს უმატებს "BulletForce"
 CurAmmoCount = CurAmmoCount - 1; //-1 ტყვია
 audio.PlayOneShot(Fire); //ხმა
 
 }
 void OnGUI()
 {
 GUI.DrawTexture(new Rect((Screen.width-AimTexture.width)/2-OffsetAimX,(Screen.height-AimTexture.height)/2-OffsetAimY,AimTexture.width,AimTexture.height), AimTexture);
 GUI.skin = $$anonymous$$ySkin;
 GUI.Box(new Rect(Screen.width/15,Screen.height-AmmoBoxHeight,AmmoBoxWidth,AmmoBoxHeight), "Ammo:"+CurAmmoCount+"/"+inventoryAmmoCount);
 }
 }
 
                 Your answer
 
             Follow this Question
Related Questions
keybind problem 0 Answers
How to put a pause in a function? 3 Answers
Problem with enemy shooting 4 Answers
Shoot Delay Problem[HELP] 1 Answer
How to make a gun shoot in burst. 2 Answers