- Home /
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?
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.
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.
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.
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.
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.
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.