Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 stulleman · May 30, 2017 at 07:41 PM · timeshootingeventtiminggeneral

Precise Timing

I need to trigger some events (e.g. shooting) in a fixed amount of time.

Let's say I have a frame rate of 60. This means 1 / 60 = 0.01666666666 seconds in each frame. Given this time resolution, it is impossible to precise time the shooting of an automatic rifle.

When I don't limit my frame rate, I get 0.10482 - 0.100992 seconds delay when desired is 0.1. When I turn on vsync, or limit my frame rate to 60, I get 0.1159 - 0.116 seconds delay.

This doesn't seem like much, but when you hear it (gun shots) it is very noticeable. Do you have an idea for frame rate independent shooting?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ShadyProductions · May 30, 2017 at 07:44 PM

"if(Time.time < 5)" is used to create a consistent test of firing rate between varying framerates.

Here are the results of the tests. This tests the number of shots fired in a 5 second period with slower and faster framerates and a 0.01 time interval between shots:

Coroutine: Slow = 93 shots, Fast = 147 shots

Invoke: Slow = 130, Fast = 193

Original: Slow = 135, Fast = 293

FixedUpdate Original: Slow = 250, Fast = 250

      // This is the Original test
      void OriginalTest()
      {
          if(CanFire ())
          {
              lastFire = Time.time;
              shotsFired ++;
          }
      }
      bool CanFire()
      {
          return Time.time >= lastFire + interval;
      }
      
      
      // This is the Invoke test
      void InvokeTest()
      {
          if(canFire)
          {
              canFire = false;
              shotsFired ++;
              Invoke ("invoke", interval);
          }
      }
      void invoke()
      {
          canFire = true;
      }
      
      
      // This is the Coroutine test
      void CoroutineTest()
      {
          if(canFire)
          {
              canFire = false;
              shotsFired ++;
              StartCoroutine(Wait(interval));
          }
      }
      
      IEnumerator Wait(float waitTime) 
      {
          yield return new WaitForSeconds(waitTime);
          canFire = true;
      }
      
  }

Results:

The FixedUpdate method appears to be the only one that works independent of the framerate. However, that creates another issue, which is that FixedUpdate tends to completely miss Input sometimes.

Also take a look at InvokeRepeating

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 stulleman · May 30, 2017 at 07:59 PM 0
Share

This unfortunately is missing my point. The fact that only FixedUpdate works is because of the fact that it is in fact frame rate independent.

I see no logical way to overcome this problem in Update, but games like battlefield do have frame rate independent shooting, as why I asked this question here.

avatar image ShadyProductions stulleman · May 30, 2017 at 08:01 PM 0
Share
 InvokeRepeating

allows you to executing things every so many seconds etc..

avatar image stulleman ShadyProductions · May 30, 2017 at 08:09 PM 0
Share

Okay my first tests show that InvokeRepeating indeed gives consistent results with varying frame rates. I'll look into it, and look if it is applicable for shooting. Thanks

avatar image
0

Answer by stulleman · May 30, 2017 at 08:22 PM

Okay thanks to @ShadyProductions I managed to get frame rate independent timing, but I don't know how it works, and it feels very hacked. Can someone explain how Invoke works?

Comment
Add comment · Show 2 · 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 ShadyProductions · May 30, 2017 at 08:26 PM 0
Share

It is explained on the wiki

InvokeRepeating

https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html

CancelInvoke

https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.CancelInvoke.html

isInvoking property

https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.IsInvoking.html

You can trigger it with booleans in the Update method to check for mousebutton input to know when you can start a InvokeRepeating (and know not to start another invoke because you are already shooting, when mouse is released then cancel the invoke (might want to also make a case for when you run out of ammo)

avatar image stulleman ShadyProductions · May 30, 2017 at 08:36 PM 0
Share

No, it explaines how to use it, not how it works.

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

66 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

Related Questions

Read and interpret data from .txt in real time, line by line 0 Answers

Action on mouse button up 1 Answer

Accurate millisecond timed thread 2 Answers

Timed actions 1 Answer

Trigger on entering field of view 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