对于我当前的项目,我正在使用Unity进行模拟并记录一些数据。它基本上只是一辆车而我正在尝试记录这些东西:
这个日志记录脚本对我的data.txt工作正常,我得到了这个结果。
1 1 0.2876698 -3.296932 95.41293
1 2.02 1.095865 -3.286392 94.23542
1 3.04 2.818792 -3.264626 92.27694
1 4.08 3.079535 -3.226411 89.4863
1 5.12 2.492892 -3.171062 85.86295
现在我想添加第二个脚本,它给了我每圈所需的时间,以便检查我们是否有一个恒定的时间。而且我必须以这种方式拥有它:圈数时间。
如果我想打印出来,我会得到这个结果:
lap time
62.02
所以我很惊讶,因为它与我的第一个数据记录器中的时间变量相同,所以我想检查出错了什么,并将第一个记录器中的相同命令放入Debug.log中,得到了相同的结果作为我的第二个记录器。
所以现在我只是想知道完全相同的线路如何能给我两个不同的输出以及如何让我的圈计时器以他应该工作的方式工作。
这是完整的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class SaveToFile : MonoBehaviour {
public GameObject Auto;
private CarEngine Engine;
public float speed;
public float maxSpeed;
public float time;
public float motorTorque;
public float breakTorque;
public float steerAngle;
public float node;
public bool IsRunning = true;
public int WaitTimer = 1;
public float xPos;
public float zPos;
public bool cancel = false;
public bool lap = false;
public float time_out;
public float time_lap;
public int lapNum;
void Start()
{
Engine = Auto.GetComponent<CarEngine>();
StreamWriter writer = File.AppendText(@"C:\Users\kaykl\Documents\Schule\Info\Data\CarControle\lap.txt");
writer.WriteLine("lap time");
writer.Close();
}
void FixedUpdate()
{
motorTorque = Engine.maxMotorTorque;
breakTorque = Engine.maxBrakeTorque;
steerAngle = Engine.maxSteerAngle;
maxSpeed = Engine.maxSpeed;
speed = Engine.currentSpeed;
time = Time.time;
node = Engine.node;
xPos = Auto.transform.position.x;
zPos = Auto.transform.position.z;
lap = Engine.lap;
time_out = time - time_lap;
if(lap == true && time_out > 5)
{
time_lap = Time.time;
lapNum = lapNum + 1;
StreamWriter writer = File.AppendText(@"C:\Users\kaykl\Documents\Schule\Info\Data\CarControle\lap.txt");
writer.WriteLine(lapNum +'+'+time_out);
writer.Close();
}
if (IsRunning == true)
{
StartCoroutine(Wait());
}
}
public IEnumerator Wait()
{
IsRunning = false;
yield return new WaitForSeconds(WaitTimer);
Output();
IsRunning = true;
}
void Output()
{
Debug.Log(node + " " + time_out + " " + speed + " " + xPos + " " + zPos);
StreamWriter writer = File.AppendText(@"C:\Users\kaykl\Documents\Schule\Info\Data\CarControle\data.txt");
writer.WriteLine(node + " " + time_out + " " + speed + " " + xPos + " " + zPos);
writer.Close();
}
}
我只是想提前感谢你们每个人,这可能是我的一个愚蠢的错误,但我现在还没有得到它。
//编辑
好的,现在这是我的工作代码。我使用了writer.flush()然后我的Debug.log是正确的但是我的lap.txt仍然是错误的。 在我将Start()内的WriteLine更改为output()后,它以某种方式工作。所以这是我现在正在使用的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class SaveToFile : MonoBehaviour {
public GameObject Auto;
private CarEngine Engine;
public float speed;
public float maxSpeed;
public float time;
public float motorTorque;
public float breakTorque;
public float steerAngle;
public float node;
public bool IsRunning = true;
public int WaitTimer = 1;
public float xPos;
public float zPos;
public bool cancel = false;
public bool lap = false;
public float time_out;
public float time_lap;
public int lapNum;
void Start()
{
Engine = Auto.GetComponent<CarEngine>();
Output();
}
void FixedUpdate()
{
motorTorque = Engine.maxMotorTorque;
breakTorque = Engine.maxBrakeTorque;
steerAngle = Engine.maxSteerAngle;
maxSpeed = Engine.maxSpeed;
speed = Engine.currentSpeed;
time = Time.time;
node = Engine.node;
xPos = Auto.transform.position.x;
zPos = Auto.transform.position.z;
lap = Engine.lap;
time_out = time - time_lap;
if(lap == true && time_out > 5)
{
time_lap = Time.time;
lapNum = lapNum + 1;
StreamWriter writer = File.AppendText(@"C:\Users\kaykl\Documents\Schule\Info\Data\CarControle\lap.txt");
writer.WriteLine(time_out);
writer.Flush();
writer.Close();
}
if (IsRunning == true)
{
StartCoroutine(Wait());
}
}
public IEnumerator Wait()
{
IsRunning = false;
yield return new WaitForSeconds(WaitTimer);
Output();
IsRunning = true;
}
void Output()
{
Debug.Log(node + " " + time_out + " " + speed + " " + xPos + " " + zPos);
StreamWriter writer = File.AppendText(@"C:\Users\kaykl\Documents\Schule\Info\Data\CarControle\data.txt");
writer.WriteLine(node + " " + time_out + " " + speed + " " + xPos + " " + zPos);
writer.Flush();
writer.Close();
}
}
任何人都可以向我解释如何仅仅引用该方法而不是将其写下来修复问题?
非常感谢你。
答案 0 :(得分:0)
现在正在运作。我添加了writer.Flush(),它使它更好,但我仍然得到了一些奇怪的数据。问题是,当我想要打印出当前一圈以及我使用这条线的时间时:
writer.WriteLine(lapNum+' '+time_out);
我完全忘记了,我不再使用Python了,所以我使用'x'来声明字符串而不是“x”。我把它改成了
writer.WriteLine(lapNum+" "+time_out);
现在它正在发挥作用。 谢谢你的帮助:)