Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lasha_an · Jun 21, 2013 at 08:12 AM · delayshoot

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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.
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lasha_an · Jun 21, 2013 at 08:33 AM 0
Share

i tried and it didnt help :((

avatar image moonstruck · Jun 21, 2013 at 08:43 AM 0
Share

Have you replaced both WaitForSeconds and Shoot with the second code snippet?

avatar image lasha_an · Jun 21, 2013 at 08:46 AM 0
Share

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);
 }
 }
 
avatar image moonstruck · Jun 21, 2013 at 08:48 AM 0
Share

Remove Shoot on the line 31 and will work. :)

avatar image lasha_an · Jun 21, 2013 at 08:56 AM 0
Share

ahh... ok i'll try it :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges