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 thesfid · Oct 10, 2013 at 07:20 PM · arraytimemathsample rate

How to execute a function X times per second.

I'm wanting to check the position of an object x times every second, and add those positions to an array. I believe the formula I have is correct, but I'm getting inconsistent and sometimes inaccurate results. I'm obviously missing something. Any help is appreciated.

 var posArray = Array(Vector3);
 var nextActionTime : float = 0.00;
 var sampleRate : float = 20; //check twenty times per second
 
 function Update () {
      if (Time.time > nextActionTime) {
           nextActionTime = Time.time + (1/sampleRate);
           GetPosition();
      }
 }
 
 function GetPosition () {
      posArray.Add(gameObject.transform.position);
 }

This should be adding a value to the posArray 20 times per second (or, once every 0.05 seconds), but like I stated above it's inconsistent. Would this "sample rate" be affected by frame rate? Is my math/logic flawed?

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

Answer by robertbu · Oct 10, 2013 at 07:45 PM

First, I doubt this code compiles since you use 'time.time' on line 7 with a lower case 't'. The likely issue is that at the time line 6 fires, you are past time. You could probably fix this by changing line 7 to:

 nextActionTime = Time.time + (1.0/sampleRate) - (Time.time - nextActionTime);

Even with this change, there is some wiggle because Update() will not necessarily happen on the nextActionTime. And there may be issues when you game drags so that deltaTime grows to greater than your 1.0/sampleRate.

But a simpler solution would be to use InvokeRepeating(). In start just do:

 InvokeRepeating("GetPosition", 0.0, 1.0/sampleRate);

Another solution would be to use some integer multiple of Time.deltaFixedTime and execute it in FixedUpdate(). By default FixedUpdate() is called 50 times per second so you could do it every other frame and get 25 samples per second.

Comment
Add comment · Show 7 · 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 thesfid · Oct 10, 2013 at 08:09 PM 0
Share

Thanks for the quick reply @robertbu, and for pointing out the typo (which isn't in my actual script, so it compiles). I'll edit my question to correct the Time.time.

I'm going to try your first suggestion first to see if that helps with consistency.

I had considered using InvokeRepeating, and I might end up going that route.

Also, thanks for the tip on FixedUpdate(). I didn't know that. $$anonymous$$y first thought is that this is the way to go, but only further testing will tell.

avatar image thesfid · Oct 10, 2013 at 08:20 PM 0
Share

nextActionTime = Time.time + (1.0/sampleRate) - (Time.time - nextActionTime);

... yields $$anonymous$$UCH better results. Thank you!

I will still try your other suggestions and see which is best for both accuracy and performance. There is potential in this project to have to track multiple objects' positions at the same time.

avatar image thesfid · Oct 11, 2013 at 02:14 PM 0
Share

Can't use InvokeRepeating in the start function. This script is activated by another script when the animation is started. So...

$$anonymous$$y thought is to make a function that only contains InvokeRepeating, and a separate function for CancelInvoke to start and stop the GetPosition method.

avatar image thesfid · Oct 11, 2013 at 05:40 PM 0
Share

Tried this in both Update and FixedUpdate and got similar results. Strangely, the inaccuracy is that there are too many data points gathered. I would think that, if anything, there would be too few points gathered.

avatar image robertbu · Oct 11, 2013 at 05:54 PM 0
Share

If you can repeat the "too many data points gathered" problem over a short period of time, then I'd write the data point and the Time.time of each point to a file. You could gather the time in a parallel list and dump all to a file on command. Then you can review the results to see if you can spot the problem. I'd start with the FixedUpdate() solution since matching it to times visually should be much easier. Note you are potentially going to get one extra for time 0.

Show more comments

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

A problem with intersection detection 1 Answer

How to move prefabs array in sine wave? 2 Answers

How to store previous locations in a list to teleport? 2 Answers

Updating an array indices dynamically 2 Answers

How to Have a Random Interval in an Array in between Sounds being Played? 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