- Home /
How to export the input history of a gama to a txt/excel file
Hi there,
I have a developed a car simulator for a university project, and i want to record the history of the players inputs. When the driver/player is playing, i want to be able to record the moments when he or she presses the brake/acelerator and for how long. Kinda like race telemetry, that would be cool. Do you guys have any ideas or suggestions? i dont have really a clue on how to do this.
Regards
Answer by mikelortega · Jun 08, 2015 at 03:49 PM
You can use System.IO.StreamWriter to write to a txt file.
For instance, you can start tweaking this code, it prints a line every time a key is pressed.
public class LogFile : MonoBehaviour
{
System.IO.StreamWriter m_LogFile;
void Start()
{
m_LogFile = new System.IO.StreamWriter(@"Log.txt");
}
void Update()
{
if (Input.anyKeyDown)
{
m_LogFile.WriteLine("Key Down in frame " + Time.frameCount);
m_LogFile.Flush();
}
}
}
Wow this looks simple! So i just add this script as component of the car prefab and im all set? What if i was to use a logitech steering wheel, could this measure the travel with which the user is pressing the acelerator/brakes?
thanks a lot mikelo!
It is not difficult to change the code to use it with a Joystick or Steering Wheel. Check Input.
Input.GetAxis("Horizontal") should give you the steering wheel position.
Wow is the code this simple? So i just have to add this code as a component of the car? Another thing, if i am adding a logitech steering wheel this would work too right? Would it also measure the intensity with which im braking/acelerating?
Thanks a lot mikelo!!!
If the pedals are progressive you can measure the intensity. Check the Input $$anonymous$$anager to know which inputs you have to log. You have more info here.
Wow really? this looks very easy...So i just have to add this script as a component of the car prefab? Another thing, for a logitech steering wheel, is there a why to know how hard the user/player is pressing the acelerator or brakes?
Thanks a lot mikelo!
Yes, it is easy. Add the script to your car prefab. I recommend you to play around with the code, get your hands dirty and then ask a more specific question.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200746im_/https://answers.unity.com/themes/thub/images/avi.jpg)