- Home /
How to stabilize variable?
Hi, I want to stabilize my variable which is heading of compass. I want something like avarange of last couple of frames. The reason of this is that heading variable is so unstable.
Answer by Bunny83 · Aug 29, 2018 at 04:48 AM
Well you can use this class i've just written. I've created two implementations of the RunningAverage, once with double values and one with Vector4 values. That way it can handle most cases.
Just create an instance of the RunningDoubleAverage or RunningVector4Average class and pass the desired number of samples you want to calculate the average over. Now just use the Add method to add a new sample each frame. Instead of using the actual input value you would use the Current property which is the average over the last X samples.
Note that the vector4 variant is not really suited for direction vectors as if calculates just the average vector which could change the length. You could renormalize the result to get back a unit vector.
So basically instead of
target = yourInputValue;
You would do
// class variable
RunningDoubleAverage average = new RunningDoubleAverage(20);
// ...
average.Add(yourInputValue);
target = average.Current;
Your answer
![](https://koobas.hobune.stream/wayback/20220612173635im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Use of the compass on Unity 0 Answers
[2D] Get 0-359 compass angle player <--> object 1 Answer
Input.compass.trueHeading sucks! 1 Answer
Help with GUI.Box Problem? 1 Answer
gyroscope/compass camera for Unity 5 0 Answers