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 Krisprolls · Mar 04, 2012 at 02:03 AM · fpstutorialfirerate

Rate of fire : explanation please

Hello,

I have some trouble to understand a piece of code related to the rate of fire of a machinegun, can someone explain it to me with simple words please ?

Here's the code I don't understand :

// If there is more than one bullet between the last and this frame
// Reset the nextFireTime
if (Time.time - fireRate > nextFireTime)
    nextFireTime = Time.time - Time.deltaTime;

The context:

function Fire () { if (bulletsLeft == 0) return;

 // If there is more than one bullet between the last and this frame
 // Reset the nextFireTime
 if (Time.time - fireRate > nextFireTime)
     nextFireTime = Time.time - Time.deltaTime;
 
 // Keep firing until we used up the fire time
 while( nextFireTime < Time.time && bulletsLeft != 0)
 {
 FireOneShot();
 nextFireTime += fireRate;
 }

}

FPS Tutorial (
MachineGun.js)

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 syclamoth · Mar 04, 2012 at 03:24 AM 1
Share

It's for guaranteeing that the correct number of bullets will always fire in a given frame, if the rate of fire is higher than the framerate. Simply, it resets the 'nextFireTime' to the previous frame if the shot should have happened several times this frame.

avatar image aldonaletto · Mar 04, 2012 at 03:54 AM 0
Share

Good explanation, @syclamoth! I wasn't thinking about a fire rate greater than the framerate, what explains perfectly this code. I edited my answer, but only God knows when it will appear in the questions page.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Mar 04, 2012 at 03:38 AM

This code increments nextFireTime by fireRate after each shot. When you stop firing, nextFireTime stops too. When shooting again, the machine gun would have to spit bullets frame after frame until the nextFireTime reached the current time. To avoid this, the second if resets nextFireTime to a little before the current time when more than fireRate seconds have elapsed since the last shot.
A simpler approach would be to add fireRate to the current time when shooting:

  if (nextFireTime  0){
    FireOneShot();
    nextFireTime = Time.time + fireRate;
  }
The fire rate in the first method is a little more precise because it doesn't accumulate time errors (what doesn't make much difference at all).
NOTE: The explanation above only considers fire rates lower than the frame rate. @syclamoth noticed an interesting feature of the first method: it allows fire rates greater than the frame rate, and that's why nextFireTime is set to the previous frame time when start shooting.
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Where is the FPS Tutorial? 1 Answer

Learning about graphics in Unity 2 Answers

are animation frame dependent? 1 Answer

Complete AI Tutorial? 1 Answer

Where is the FPS Tutorial? 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