Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Is there a way to stabilise jittering values?

Discussion in 'Scripting' started by wijdoendingen, May 26, 2015.

  1. wijdoendingen

    wijdoendingen

    Joined:
    May 23, 2015
    Posts:
    5
    Hi everyone,

    I am working on a project where I am reading the value of an analog input to control an object in my unity scene.
    I am turning a controller and reading the value it gives me. The trouble I am having is that the values sometimes fluctuate between two values.

    For example: sometimes my analog value (pinValue) fluctuates rapidly between 250 and 251. This causes my object (that is attached to that value) to jitter and shake.

    Is there a way to “smooth” or “round off” these values so the jittering stops?

    It would be great if there were a way not to log values that only jump 1 value up or down. Or only log values that jump with an amount greater then 2 or 3.

    This is my code in c#:


    Code (CSharp):
    1.       public int pinValue;
    2.         public float spinSpeed;
    3.  
    4.         void Start () {
    5.  
    6.             // name of gameObject
    7.             xcamera = GameObject.Find("Camera");
    8.         }
    9.      
    10.      
    11.         }
    12.      
    13.         void Update () {
    14.             // analog input
    15.             pinValue = arduino.analogRead(pin);
    16.          
    17.              // rotate the object along the x-axis
    18.             xcamera.transform.rotation = Quaternion.Euler(0,pinValue*spinSpeed,0);
    19.  
    20.         }
    Does anybody know how I can achieve this? Help would be gratefully appreciated.
     
  2. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    SmoothDamp will lessen the issue, but I think it'll still feel a little bit jittery. The idea posited where it ignores values that jump by only 1 is the best way to proceed IMO.

    That code might look something like this:
    Code (csharp):
    1.  
    2. public int rawValue = 0;
    3. public int lagValue = 0;
    4.  
    5. void Update() {
    6. rawValue = arduino.analogRead(pin);
    7. if (lagValue < rawValue) lagValue = rawValue - 1;
    8. if (lagValue > rawValue) lagValue = rawValue + 1;
    9. //otherwise, it's already equal, do nothing
    10. xcamera.transform.rotation = Quaternion.Euler(0,lagValue*spinSpeed,0);
    11. }
    12.  
    You may want to apply additional smoothing on *top* of this algorithm, to prevent it from jumping noticeably, but this should prevent it from jittering back and forth.
     
  4. wijdoendingen

    wijdoendingen

    Joined:
    May 23, 2015
    Posts:
    5
    Wow, Thank you very much StarManta for this answer! It really helps stabilising my values and works great! Your reply is greatly appreciated.

    I will try and combine this with DoomSamurai answer. Thank you as well.