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 esitoinatteso · Oct 06, 2013 at 08:47 PM · c#firerateof1 frame

Fooling Rate Of Fire C#

Hi again! I'm trying to prevent my player from shooting as fast as he can press the Space Key. I'm programming in C# right now and I find Coroutines a little bit too complex to handle. Instead, what I came up with is this:

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     public GameObject myProjectile;
     public float nextFire = -1.0f;
 
 void Start () {
         nextFire = Time.time;
         
     }
 
 void Update () {
         
         //SHOOTING
         if(Time.time >= nextFire){
             
             if(Input.GetKeyDown(KeyCode.Space)){
                 nextFire = nextFire+1;
             myProjectile.transform.position = new Vector3 ( Player.position.x, Player.position.y, Player.position.z);
                 Instantiate(myProjectile);
             }
             
         }
        }
 }

This script works good in my 2d game project, except for one issue: Sometimes, when I press Space really really fast, I can shoot two or three times in a row no matter what the code says... especially when I intentionally pick up the peace to fool this limitation.

I tried to change the GetKeyDown with GetKey or GetKeyUp and nothing changes for the good.

It seems my solution is either wrong or incorrectly applied. Is this oversight somehow implied by my code and nothing can be done to solve this issue, apart from adopting a new solution? In this case, what would you suggest?

Thanks for your time!

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
3
Best Answer

Answer by robertbu · Oct 06, 2013 at 08:52 PM

The issue I see is not so much with rapid keystrokes, but one of how long you pause before you fire. Let me start with my suggested solution. Line 20 should be:

 nextFire = Time.time + 1.0;

This means the user will have to pause one full second before firing again. Without this, imagine the player waiting 10 seconds before hitting the 'Space' key. He would then be aboe to fire 10 shots as fast as he could before nextFire would catch up to Time.time.

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 esitoinatteso · Oct 06, 2013 at 09:55 PM 0
Share

Ah! thanks a lot for pointing that out! it works perfectly now!

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

rapid movement back and forth 1 Answer

Renderer on object disabled after level reload 1 Answer

Working projectile scripts do not work due to if condition 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