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
1
Question by HercULAS · Mar 16, 2014 at 04:12 PM · c#shootingbulletdelay

Shot Delay between button press. c#

I'm a noob, but I've looked around on the forums and stuff and even though there are solutions i just cant seem to get a grip with how they work. Maybe because the way I'm setting up my shooting method is different. Basically its like a space invaders type of game and when the player hits space bar it will instantiate a bullet(prefab), however they can just spam the button and make the game that much more easier. I want there to be a limit to how fast you can press the space bar and it to shoot. sort of like a delay maybe? I'm not sure of the exact term.

Here's the code-

 using UnityEngine;
 using System.Collections;
 
 public class playerShoot : MonoBehaviour 
 
 {
     public GameObject bullet;
     
 
     // Use this for initialization
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         if ((Input.GetKeyDown(KeyCode.Space)) )
         {
         Instantiate(bullet, transform.position, transform.rotation);
         }
     
         
     }
 }

please can anybody help? thanks.

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
24

Answer by robertbu · Mar 16, 2014 at 04:24 PM

A simple solution is what I call a timestamp. Note there is a tiny bit of inaccuracy in the calculation but used for this, it won't be noticed by the user:

 using UnityEngine;
 using System.Collections;
 
 public class playerShoot : MonoBehaviour 
 {
     public GameObject bullet;
     public float timeBetweenShots = 0.3333f;  // Allow 3 shots per second
 
     private float timestamp;
 
     void Update () 
     {
         if (Time.time >= timestamp && (Input.GetKeyDown(KeyCode.Space)) )
         {
             Instantiate(bullet, transform.position, transform.rotation);
             timestamp = Time.time + timeBetweenShots;
         }
     }
 }

 
Comment
Comments Locked · 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 HercULAS · Mar 16, 2014 at 05:26 PM 1
Share

ohh thanks! it seemed to have worked!

avatar image AurimasBlazulionis · Sep 11, 2016 at 09:58 AM 0
Share

Do not forget to accept answers if they are correct. Also, sometimes, if you need to have more correct time limit, you will need to move from Update to FixedUpdate and change Time.time with Time.fixedTime.

avatar image MD_Sky · Jan 01, 2018 at 08:32 PM 0
Share

So how would you ^ / \/ the time delay?, if it's not much trouble could you explain the 0.3333f part because i would like to know if we are able to customise the ti$$anonymous$$g, i got the shooting working, and since my game is in a demo phase i don't $$anonymous$$d but xD you know i'm interested now.

Again thanks for the code.

avatar image
1

Answer by larry2013z · Jun 27, 2020 at 08:04 PM

//Here is another way where you only need to run Time.time when the player presses the spacebar and not every frame

public class PlayerControllerX : MonoBehaviour { public GameObject dogPrefab; public static float time; public float timeLastPass = 0.0f;

 // Update is called once per frame
 void Update()
 {
     // On spacebar press, send dog
     if (Input.GetKeyDown(KeyCode.Space))
     {
         time = Time.time; //The time at the beginning of this frame (Read Only). This is the time in seconds since the start of the game.
         float deltaTime = time - timeLastPass; //The time since the spacebar was successfully used
         if (deltaTime > 2.0f)
         {
             Instantiate(dogPrefab, transform.position, dogPrefab.transform.rotation);
             timeLastPass = time;
         }

     }
 }

}

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 AlejandroBoss10 · Jun 27, 2020 at 10:40 PM

I see other people have done some things but I'm going to add my 2 cents. If you have timeBetweenShots set to .25, the player has to wait .25 of a second in order to shoot again. If you have it set to 5, they have to wait 5 seconds before being able to shoot again. Hopefully this helps you out and anyone else who is looking for something like this.

 public GameObject bullet;
 public float timeBetweenShots;
 private bool canShoot;


 // Use this for initialization
 void Start()
 {
     canShoot = true;
 }

 // Update is called once per frame
 void Update()
 {
     if ((Input.GetKeyDown(KeyCode.Space) && canShoot))
     {
         Shoot();
     }
 }

 void Shoot()
 {
     print("Shot Fired");
     canShoot = false;
     Instantiate(bullet, transform.position, transform.rotation);
     StartCoroutine(ShotCooldown());
 }

 IEnumerator ShotCooldown()
 {
     yield return new WaitForSeconds(timeBetweenShots);
     canShoot = true;
 }
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

32 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

Related Questions

Invoke shoot delay problem. 3 Answers

adding a delay to shooting 2 Answers

Multiple Cars not working 1 Answer

delay between shots 1 Answer

Distribute terrain in zones 3 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