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 JA_555 · Mar 27, 2015 at 01:25 AM · updatelateupdate

calculate a falling number

How should I go about calculating the speed of number that drops at a variable rate. I think the rate should be calculated in lateupdate, while the number would be changing during update.

It would probably be the difference between a previous number and the current number, but the part I don't know how to do is calculate the previous number. If you have any ideas let me know

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

Answer by ian_sorbello · Mar 27, 2015 at 03:05 AM

I often do these things in a CoRoutine - rather than between Update and LateUpdate etc. This also means you can calculate rates very efficiently with low overhead - because you don't always need to calculate this every frame.

Here's how I'd do it:

 public class CalcRate : MonoBehaviour {
 
     public float Rate; // This is the answer you want - the rate of change over time
     public float Frequency = 0.1f;  // How often you want this to be calculated - smaller = more accurate
     public float ValueToTrack;  // The value that is changing - your code would just update this at any time
 
     private float _sampleTime;
     private float _previousValue;
     
     void Start () {
         // Start up the coroutine
         StartCoroutine(CalculateRate());
     }
 
     IEnumerator CalculateRate() {
         for(;;) {
             // Record the current Value
             _previousValue = ValueToTrack;
             _sampleTime = Time.time;
 
             // Wait a little bit
             yield return new WaitForSeconds(Frequency);
         
             // Calculate the rate - difference divided by amount of time passed
             float diff = ValueToTrack - _previousValue;
             Rate = diff / (Time.time - _sampleTime);
         }
     }
 
     // Update is called once per frame
     void Update () {
         // Don't need this
     }
 
     void OnDestroy() {
         StopAllCoroutines();
     }
 }


Some might argue this is overkill ... But I think the benefit of being able to change the frequency to something dialled down (low rate) or something dialled up (high rate) gives you the accuracy you need, and saving lots of CPU.

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

22 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

Related Questions

Update/Late Update jitter problem(non FixedUpdate related) 1 Answer

Should terrain detail data be set on update, fixedupdate, lateupdate or coroutines? 0 Answers

Calculating change in mouse position 2 Answers

The latest update is too old for some assets? 2 Answers

How can I get my camera to momentarily pause between different positions when using lerp? 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