• 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 naming 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How to Instantiate prefabs with function Update? 1 Answer

Trigger multiple functions from instantiated objects when pressing a UI Button. 0 Answers

Function that Runs Once? 2 Answers

Instantiate only one prefab out of 3 prefabs on a button click during runtime 0 Answers

Networking already instantiated assets 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges