在此项目中,存在一个随机移动的对象,必须将其坐标打印到文件中。为了做到这一点,我创建了一个名为PrintPoints()的函数。但是,我不知道在哪里调用PrintPoints(),就好像我在Update()方法中调用它一样,它会产生冗长而荒谬的输出,我相信这是因为Update()在每一帧都运行。我只需要运行Update()并在对象完成移动(程序的最后)后打印坐标。
我该如何实现?
非常感谢您!
PS:以下是我的代码
public class PlayerController : MonoBehaviour {
bool hasArrived;
private float movementDuration = 2.0f;
private float waitBeforeMoving = 2.0f;
private Vector3[] v = new Vector3[20];
//StreamWriter file = new StreamWriter("output.txt",true);
void Update()
{
if (!hasArrived)
{
hasArrived = true;
StartCoroutine(MoveToPoints());
}
}
private IEnumerator MoveToPoints()
{
for (int i = 0; i < v.Length; i++)
{
float timer = 0.0f;
Vector3 startPos = transform.position;
float x = RandomNum(timer);
float y = RandomNum(x);
float z = RandomNum(y);
v[i] = new Vector3(x, y, z);
while (timer < movementDuration)
{
timer += Time.deltaTime;
float t = timer / movementDuration;
t = t * t * t * (t * (6f * t - 15f) + 10f);
transform.position = Vector3.Lerp(startPos, v[i], t);
yield return null;
}
yield return new WaitForSeconds(waitBeforeMoving);
}
}
void PrintPoints()
{
//path of file
string path = Application.dataPath + "/Player.txt";
//create file if nonexistent
if(!File.Exists(path))
{
File.WriteAllText(path, "The player blob visited these random coordinates: \n\n");
}
foreach(Vector3 vector in v)
{
File.AppendAllText(path, "(" + vector.x + ", " + vector.y + ", " + vector.z + ")\n\n");
}
}
float RandomNum(float lastRandNum)
{
//Random value range can be changed in the future if necessary
float randNum = Random.Range(-10.0f, 10.0f);
return System.Math.Abs(randNum - lastRandNum) < double.Epsilon ? RandomNum(randNum) : randNum;
}
}
答案 0 :(得分:0)
在此程序中,必须在调用MoveToPoints()之后调用一次PrintPoints(),因为您将矢量中的所有点打印出来。 更好的方法是,在调用PrintPoints时仅打印一个点。我将Point作为该方法的参数打印出来,例如:
void PrintPoint(Vector3 vector)
{
//path of file
string path = Application.dataPath + "/Player.txt";
//create file if nonexistent
if(!File.Exists(path))
{
File.WriteAllText(path, "The player blob visited these random coordinates: \n\n");
}
File.AppendAllText(path, "(" + vector.x + ", " + vector.y + ", " + vector.z + ")\n\n");
}
答案 1 :(得分:0)
Update()在每一帧运行。如果只希望一次调用MoveToPoints()方法,则可以改用Start()方法调用它。如果这样做,则可以在调用MoveToPoints()方法之后调用PrintPoints()方法。
这看起来像这样:
public class PlayerController : MonoBehaviour {
private float movementDuration = 2.0f;
private float waitBeforeMoving = 2.0f;
private Vector3[] v = new Vector3[20];
//StreamWriter file = new StreamWriter("output.txt",true);
void Start()
{
this.MoveToPoints();
this.PrintPoints();
}
private void MoveToPoints()
{
for (int i = 0; i < v.Length; i++)
{
float timer = 0.0f;
Vector3 startPos = transform.position;
float x = this.RandomNum(timer);
float y = this.RandomNum(x);
float z = this.RandomNum(y);
v[i] = new Vector3(x, y, z);
while (timer < movementDuration)
{
timer += Time.deltaTime;
float t = timer / movementDuration;
t = t * t * t * (t * (6f * t - 15f) + 10f);
transform.position = Vector3.Lerp(startPos, v[i], t);
yield return null;
}
}
}
void PrintPoints()
{
//path of file
string path = Application.dataPath + "/Player.txt";
//create file if nonexistent
if(!File.Exists(path))
{
File.WriteAllText(path, "The player blob visited these random coordinates: \n\n");
}
foreach(Vector3 vector in v)
{
File.AppendAllText(path, "(" + vector.x + ", " + vector.y + ", " + vector.z + ")\n\n");
}
}
float RandomNum(float lastRandNum)
{
//Random value range can be changed in the future if necessary
float randNum = Random.Range(-10.0f, 10.0f);
return System.Math.Abs(randNum - lastRandNum) < double.Epsilon ? RandomNum(randNum) : randNum;
}
}
答案 2 :(得分:0)
public class PlayerController : MonoBehaviour
{
float movementDuration = 2.0f;
WaitForSeconds waitBeforeMoving = new WaitForSeconds( 2f );
Vector3[] path = new Vector3[20];
void Start ()
{
StartCoroutine( MainRoutine() );
}
IEnumerator MainRoutine ()
{
//generate new path:
for( int i=0 ; i<path.Length ; i++ )
{
float timer = 0.0f;
Vector3 startPos = transform.position;
float x = RandomNum( timer );
float y = RandomNum( x );
float z = RandomNum( y );
path[i] = new Vector3(x, y, z);
}
//traverse path:
for( int i=0 ; i<path.Length ; i++ )
{
float timer = 0.0f;
Vector3 startPos = transform.position;
while( timer<movementDuration )
{
timer += Time.deltaTime;
float t = timer / movementDuration;
t = t * t * t * (t * (6f * t - 15f) + 10f);
transform.position = Vector3.Lerp( startPos , path[i] , t );
yield return null;
}
yield return waitBeforeMoving;
}
//print path:
PrintPoints();
}
void PrintPoints ()
{
string filepath = Application.dataPath + "/Player.txt";
if( File.Exists( filepath )==false )
{
File.WriteAllText( filepath , "The player blob visited these random coordinates: \n\n" );
}
foreach( Vector3 vector in path )
{
File.AppendAllText( filepath , $"{ JsonUtility.ToJson(vector) }\n\n" );
}
}
float RandomNum ( float lastRandNum )
{
//Random value range can be changed in the future if necessary
float randNum = Random.Range(-10.0f, 10.0f);
return System.Math.Abs(randNum - lastRandNum) < float.Epsilon ? RandomNum(randNum) : randNum;
}
}