So I have this function in my racing game that calculates the speed at which the player is moving. It’s fairly simple. This works great most of the time, but occasionally it’ll output a completely outlandish number that isn’t anywhere close to my actual speed, seemingly at random, but usually goes back to normal after a frame or two. Is there a way I could work around this? I’m currently working in UE 4.25.4.
I’m wondering if your problem is because you’re trying to measure speed per frame. Although the time taken to render a frame can be pretty steady, it can vary a lot, especially if an asset is suddenly brought into view etc.
Why not measure it per second?
As ClockworkOcean stated frame rates fluctuate. The degree of variance depends on scene and hardware. You can have what appears to be a solid 60, but also have intermittent 20’s or lower. read up on 1% lows.
There’s quite a few ways to get around the issue depending on its use. You can clamp the value at output. You can use modulus and only check it at intervals. You can also use both the previous options, average the returns over the past second and update once per second.
For example if this for a Hud element I’d only update the speed once per second or 500ms. Have a check function calc current speed once ever 5/10 frames, store the clamped float in an array. At the update interval get an average of the values in the array, clear the array, pass the value to umg.
1% lows vid 1% & 0.1% Lows Explained! - YouTube
I figured this was the case. After messing with it a few days I got it working, although instead of having an array, I stored it as a single added float value. I figured this would save on performance since I wouldn’t have to go through a for each loop every frame. Storing it as an array would’ve also added unnecessary complexity. Either way, thanks for the advice.