Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 yaezah · Aug 19, 2017 at 02:57 AM · instantiateunity5functionfunction update

Bullet is shooting every frame? How do I limit it to 1 bullet per second?

I'm trying to change a script to shoot 1 bullet per second on mouse being held. The void update and IEnumerator was added by me , the fire() function is the script..

 void Update(){

     if (Input.GetMouseButton (0)) {

         StartCoroutine (FireShot ());
     }

 }
     
 IEnumerator FireShot() {

     Fire ();
     yield return new WaitForSeconds (1);
 }


 public void  Fire()
 {
         Vector3 mousePoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         transform.up = Vector3.Normalize (mousePoint + Vector3.forward * 10 - transform.position);
         if (PlayerPrefs.GetInt ("gold", 200) < _levelGun && _tenlua == false)
             popUp.SetActive (true);
         else {
             if (PlayerPrefs.GetInt ("gold", 200) >= _levelGun && _checkfire && _tenlua == false) {
             
                 _ani.Play ("Fire", 0, 0);
                 AudioControl.Instance.shoot ();
                 GameObject _bullet = (GameObject)Instantiate (Bullet);
                 _bullet.transform.position = transform.position + transform.up * 0.5f;
                 _bullet.GetComponent<BulletControl> ().InitBullet (_levelGun, transform, new Vector2 (mousePoint.x, mousePoint.y));

                 UiTextSpawmControl.Instance.MinusGold (_levelGun);

             }
         }
         if (_tenlua && _checkfire) {
             _tenlua = false;
             tenlua.transform.up = Vector3.Normalize (mousePoint + Vector3.forward * 10 - tenlua.transform.position);
             _checkfire = false;
             LeanTween.move (tenlua, new Vector3 (mousePoint.x, mousePoint.y, 0), 0.2f * (Vector2.Distance (mousePoint, tenlua.transform.position))).setOnComplete (() => {
                 RaycastHit2D[] fish = Physics2D.CircleCastAll (new Vector3 (tenlua.transform.position.x, tenlua.transform.position.y, 0), 2, Vector3.zero);
                 AudioControl.Instance.boom ();
                 for (int i = 0; i < fish.Length; i++) {
                     if (fish [i].collider.tag == "fish")
                         fish [i].collider.gameObject.GetComponent<FishControl> ().hitDame (1000, gameObject);
                 }
                 GameObject boom = (GameObject)Instantiate (_effboom, tenlua.transform.position + tenlua.transform.up * 0.5f, Quaternion.identity);
                 Destroy (boom, 1.5f);
                 tenlua.SetActive (false);
                 GetComponent<SpriteRenderer> ().enabled = true;
                 transform.up = Vector3.up;
                 transform.localScale = Vector3.zero;
                 LeanTween.scale (gameObject, new Vector3 (1, 1, 1), 0.5f).setEase (LeanTweenType.easeOutBack).setOnComplete (() => {
                     _checkfire = true;
                 });
             });
         }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by yaezah · Aug 19, 2017 at 03:48 AM

If anyone has a problem like this this is how I fixed it:

Add a bool "canshoot=false"

then change the update and IEnumerator to this:

     void Update(){
 
         if ((Input.GetMouseButton(0))&&(!canshoot)) {
 
             StartCoroutine (FireShot ());
         }
 
     }
         
     IEnumerator FireShot() {
         
             canshoot = true;
             Fire ();
             yield return new WaitForSeconds (1);
             canshoot = false;
 
     }
 
Comment
Add comment · Show 1 · 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 unity_DsjUSX41brxEGw · Feb 03, 2020 at 02:49 PM 0
Share

I like this answer because this is what I would do, except I would suggest na$$anonymous$$g the variable something different. I$$anonymous$$O checking for "canshoot == false" seems like you're checking "If I can't shoot... Then shoot." Just for readibility, I think. Otherwise it's perfect.

avatar image
1

Answer by G4M3R72 · Feb 03, 2020 at 10:41 AM

That code works great for me too yaezah. I created a public float for fire rate and put it in the IEnumerator so I can change the rate for each scene as the person levels up... yield return new WaitForSeconds(fireRate);

Comment
Add comment · 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
1

Answer by Nk_Khumbhani · Feb 03, 2020 at 11:14 AM

Decrease the delay time from 1 to 0.5f is worked for me... IEnumerator FireShot() {

     canShoot = true;
     Fire();
     yield return new WaitForSeconds (0.5f);
     canShoot = false;
     

}

Comment
Add comment · 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

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

93 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Execute IF statement once inside the Update Function? 4 Answers

Object coordinates not updating as expected 0 Answers

main camera destroying in editor but not in android 1 Answer

Unet NetworkServer.Spawn() not working 5 Answers

Instantiated Prefab not calling Start() nor Awake() 1 Answer


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