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. Dismiss Notice

Question Calculating the rate of change of a variable in the Update loop

Discussion in 'Scripting' started by Cato11, Sep 21, 2023.

  1. Cato11

    Cato11

    Joined:
    Jan 5, 2021
    Posts:
    193
    Does anyone know how it might be possible to calculate the rate of change of a variable in the update loop? Consider an example below:

    Code (CSharp):
    1. void Update()
    2. {
    3.   // Sample instantaneous values
    4.   gravityX = gyro.gravity.x;  
    5.   gravityY = gyro.gravity.y;
    6.   gravityZ = gyro.gravity.z;
    7.  
    8.   // How to calculate the rate of change of these values?
    9.  
    10. }
    In the update loop I am sampling these three instantaneous values from the device's gyroscope. But I want to see whether these values are changing, and if so, what is the rate of change. Does anyone know how this can be calculated?
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1.     Vector3 previousGravity;
    2.  
    3.     void Update()
    4.     {
    5.       Vector3 changeRate=(gyro.gravity-previousGravity)*Time.detaTime;
    6.       previousGravity=gyro.gravity;
    7.     }
     
    Cato11 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I don't think this is quite right...

    I think you mean to divide by Time.deltaTime to result in a time-stabilized "per second" value.

    Essentially the two values (prev and curr) are already implicitly separated in time by the deltaTime, so we divide it out.

    This also means you should guard Time.deltaTime for being greater than like 0.001f or so... in that case just return
    Vector3.zero
    for
    changeRate
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Oops!. I actually meant to multiply by frame rate and not frame time.
     
    Last edited: Sep 22, 2023
  5. Cato11

    Cato11

    Joined:
    Jan 5, 2021
    Posts:
    193
    Could you possibly post what you think is the correct solution?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Just zulo's solution, with a / instead of a *.

    Code (CSharp):
    1. Vector3 changeRate = (gyro.gravity - previousGravity) / Time.detaTime;
     
    Cato11 likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Here you go, instantaneous gravity change (hijacking Zulo's original code):

    Code (csharp):
    1.     public Vector3 instantaneousGravityChange;   // value is here for you to read out
    2.  
    3.     Vector3 previousGravity;  // previous frame
    4.  
    5.     void Start()
    6.     {
    7.         previousGravity = gyro.gravity;   // avoid first frame false spike
    8.     }
    9.     void Update()
    10.     {
    11.  
    12.       float deltaTime = Time.deltaTime;
    13.  
    14.       Vector3 gravity = gyro.gravity;
    15.  
    16.       // presume no change to handle when game paused
    17.       // note: depending on your needs, you may wish to exit early
    18.       // if deltaTime is too small (see check below) and leave the
    19.       // instantaneousGravityChange at its last computed value.
    20.       // in that case, just comment out this line:
    21.       instantaneousGravityChange = Vector3.zero;
    22.  
    23.       // prevent divide by zero if the game should pause
    24.       if (deltaTime > 0.001f)
    25.       {
    26.           instantaneousGravityChange = (gravity - previousGravity) / deltaTime;
    27.       }
    28.  
    29.       // age data
    30.       previousGravity = gravity;
    31.     }
     
    Cato11 likes this.
  8. Cato11

    Cato11

    Joined:
    Jan 5, 2021
    Posts:
    193
    Thanks so much guys, it works! Really appreciate the help!