Is there a way to save the player data (i.e. position) every 1 second automatically while he is playing?

Basically, I am creating a simple game for my project. I should store each player’s data (i.e., position or velocity) at every time steps that I need (e.g., every 0.02 seconds) in a .txt file as an example and save it. I should do it since I need to use these data for post processing using other mathematical softwares like MATLAB. However, I do not have any experience with Unity. I followed a simple tutorial and designed my game, but I do not know how to save the data in the way that I want. I googled a lot and the only information that I find is about saving data like score and … from one level to another level or saving data “Manually” by clicking a save button. Please note that I need to store the position of subject at every 0.02 seconds and then save it. Is there anyone that could help me with this or at least tells me how can I do it?
Thanks

From what you wrote I assume you know how to save your data, as in write a function that will gather all the information you need and save it to a file.

What you need to do is create a script that derives from MonoBehaviour. You now have access to Update which is automatically called every single frame.

Now you want to create a float variable (outside of Update) that will be used as a ‘timer’.

In Update add Time.DeltaTime to the timer until it’s bigger than your required time step. When it is, just reset the timer and save your game. Example:

public class Saver : MonoBehaviour
{
	private const float TimeStep = 0.02f; //this is in seconds
	private float _timer = 0;
	
	private void Update()
	{
		timer += Time.DeltaTime;
		if(timer >= TimeStep)
		{
			timer = 0;
			Save();
		}
	}
}

What you may want to consider is storing your position or other data in an array or other structure and only save to file once in a while to reduce performance hit from saving. Of course this may not be necessary depending on the rest of your program or you may just not want to do that, it’s just a suggestion.