Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by breed_unity848 · Mar 27, 2020 at 10:28 PM · timerdelaybulletsgun script

Bullet delay after shooting so you can't repeatedly shoot by clicking over and over,Wait for Bullet Counter to Reset before Shooting Again

Hi, I'm following a tutorial online for a shooter game. Bullets are delayed using a float variable, but only if you're holding down the "left-click". If you repeatedly click the left-click button as fast as you can, the bullets will leave the gun just as fast--bypassing the delay.

Is there a way to shoot ONLY if the delay counter is finished?

Here's the code! Hopefully we don't need to scrap the whole code for this. I do like the simplicity. Thanks in advance.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GunController : MonoBehaviour
 {
     public bool isFiring;
     public BulletController bullet;
     public float bulletSpeed;
     public float timeBetweenShots;
     private float shotCounter;
     public Transform firePoint;
 
     void Start()
     {
         
     }
 
     void Update()
     {
         if (isFiring)
         {
             shotCounter -= Time.deltaTime;
             if (shotCounter <= 0)
             {
                 shotCounter = timeBetweenShots;
                 BulletController newBullet = Instantiate(bullet, firePoint.position, firePoint.rotation) as BulletController;
                 newBullet.speed = bulletSpeed;
             }
         }
         else
         {
             shotCounter = 0;
         }
     }
 }
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
1
Best Answer

Answer by Hellium · Mar 27, 2020 at 10:32 PM

 public class GunController : MonoBehaviour
 {
     public bool isFiring;
     public BulletController bullet;
     public float bulletSpeed;
     public float timeBetweenShots;
     private float nextFireTime;
     public Transform firePoint;
     
     private bool CanFire
     {
         get { return Time.time > nextFireTime; }
     }
 
     void Update()
     {
         if (isFiring && CanFire)
         {
             nextFireTime = Time.time + timeBetweenShots;
             BulletController newBullet = Instantiate(bullet, firePoint.position, firePoint.rotation) as BulletController;
             newBullet.speed = bulletSpeed;
         }
     }
 }


Comment
Add comment · Show 3 · 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 breed_unity848 · Mar 28, 2020 at 01:53 PM 0
Share

Thank you that worked perfectly! Thanks for cleaning up the code a bit too. Any way you could explain why this works? For me and any other newbies that may find this question?

avatar image Hellium breed_unity848 · Mar 28, 2020 at 02:12 PM 0
Share

If I have to explain my code, it means the latter was not clear enough, and that's a pity.


Basically, ins$$anonymous$$d of handling a "countdown" which is decreased each time you try to fire, I take advantage of the time naturally elapsing. nextFireTime represents the time after which you will be able to fire again. Each time you shoot, you move timeBetweenShots seconds later.


alt text

On the figure above, you will be able to shoot at any point in time as long as you are in the green zone (i.e, when CanFire returns true)

avatar image breed_unity848 Hellium · Mar 28, 2020 at 06:13 PM 0
Share

Thanks much for going into that detail.

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

203 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 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 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

I've been trying to make a gun script, but somehow the boolean to tell when the bullet is destroyed doesent work 1 Answer

script delay ("on mouseclick") 0 Answers

How to scale object when timer ends? 0 Answers

Timer randomly not working. I need of assistance. C# 0 Answers

Set a delay for firing 0 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