Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Paricus · Aug 04, 2015 at 08:17 PM · programminggunsfire rate

Making Fire Rate for an Input.GetButton

Hi Guys.

I've tried every thing i can but any method i use i get the same result.


 public int damage = 10;
 public float firerate = 0.5f;
 public float nextfire = 0.0f;


 void Shoot(){

     if (Input.GetMouseButton (0) && Time.time > nextfire ) {

         nextfire =  Time.time + firerate;
     
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay (new Vector3 (Screen.width * 0.5f, Screen.height * 0.5f, 0f));
         
         if (Physics.Raycast (ray, out hit, 10000)) {
             hit.transform.SendMessage ("applydamage", damage, SendMessageOptions.DontRequireReceiver);

             Debug.Log("BANG!");
         }
     }
         

     }

i'm calling the shoot function every frame but all i get in the log it gets called 4 times roughly every second. why is it being called 4 times each log? also the sendmessage ("applydamge") isn't being called properly.

am i doing doing something wrong? is there any other method i could try?

thanks

Comment
Add comment · Show 2
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 Firedan1176 · Aug 04, 2015 at 10:20 PM 2
Share

Can you try this? It usually helps me.

 public int damage = 10;
 
 public float fireRate;
 float countdown;
 
 void Update() {
     if (Input.GetButton (0) && countdown <=0) {
 
         //Do stuff
 
         countdown = fireRate;
     }
     countdown -= Time.deltaTime;
 
 }

What this does is set the temp variable 'countdown' to a 'read-only' like variable 'fireRate'. The countown is subtracted by Time.deltaTime, which is the time (seconds) since the last frame (this way if you lag, it still keeps that time). Once countdown gets to 0, then it fires.

Depending on what kind of gun, a machine gun may be 0.08 - 0.1 (10 shots per second).

Tell me what happens.

Edit: Are you sure you don't have the script on any other Game Objects in your scene? Only thing I can think of besides what I posted above.

avatar image Paricus · Aug 08, 2015 at 08:06 PM 0
Share

thanks! it works perfectly. sorry i took so long to reply. I would up vote your answer but i cant find the option.

Thanks again.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by stulleman · Aug 04, 2015 at 11:40 PM

Try Input.GetMouseButtonDown(0).

Also you should check for Time.time >= nextfire, not Time.time > nextfire.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Inheritance in C# and Unity3d 1 Answer

Mouse targeting error 1 Answer

How do I seed the random number generator myself and still get pseudorandom values? 2 Answers

How much do Unity programmers charge/hour? 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