Is there a way to get the old version of a changing variable and store it in a new variable

I have a constantly changing variable which I need to store the last version of and get the average of the changing variable and the last version of it like for example float player_x_position = transform.position.xAnd the player can constantly move so I need to get the player_x_position and the last player_x_position and get the average of them and store it in a variable please if u know what can I do let me know.

Hi! Good question. The best way to do this would be to store this information in an array. And then figure out the average from that. Don’t let the array bloat though. If you explain more what you are trying to do and maybe show your code, I could probably help you further. Otherwise, the simple answer is to use an array.

Take advantage of the fact that the Update() function for MonoBehaviour objects is called every frame:

private float lastXPosition;

void Start()
{
    lastXPosition = transform.position.x;
}

void Update()
{
    float average = (lastXPosition + transform.position.x) / 2;
    // Do whatever stuff you need with the average
    lastXPosition = transform.position.x;
}  

Obviously this doesn’t work on the first frame, when lastXPosition is still equal to the current x position. On every subsequent frame, this will give the correct value.