unity update skipping calculation in a frame
unity update skipping calculation in a frame
我正在实例化瓦片作为路径,在每四个实例化的瓦片上,它们应该开始向右实例化,在四个实例化之后,它们再次直走。
为了检查这个逻辑,我创建了一个变量 spawnOffset 并且每帧递增 +1。
if spawnOffset % 4 == 0 改变方向
但是,我没有定期改变方向,当我调试时,跳帧,逻辑也是如此
public GameObject go;
public Transform Playertransform;
public Vector3 tileSpawnOffset = Vector3.zero;
private Vector3 direction = Vector3.forward;
public int SpawnOffset = -3;
private bool turnRight = false;
void Update()
{
SpawnPath();
ChangeDirection();
}
void SpawnPath()
{
if (Playertransform.position.z > SpawnOffset)
{
tileSpawnOffset += direction;
Instantiate(go, this.transform.position + tileSpawnOffset, this.transform.rotation, this.transform);
SpawnOffset++;
}
}
void ChangeDirection()
{
if (SpawnOffset % 4 == 0)
{
turnRight = !turnRight;
}
direction = turnRight == true ? Vector3.right : Vector3.forward;
}
但是当我尝试用时间代替时
float time = 0f;
void ChangeDirection()
{
time += Time.deltaTime;
if (time > 1)
{
turnRight = !turnRight;
time = 0;
}
direction = turnRight == true ? Vector3.right : Vector3.forward;
}
它工作得很好。所以,我该如何解决它。我,不想使用时间,我想在生成 4 个瓷砖后恰好改变方向
因为你总是同时调用 SpawnPath();改变方向(); everyframe if (Playertransform.position.z > SpawnOffset) is ever false in false twice your turnright bool gets flipped regardless every frame you on a multiple of 4. 如果你没有生成新的图块,则不需要调用 ChangeDirection .如果您只是从更新中删除该调用并在增量 SpawnOffset 之后立即添加它,它可能会修复它。
我正在实例化瓦片作为路径,在每四个实例化的瓦片上,它们应该开始向右实例化,在四个实例化之后,它们再次直走。
为了检查这个逻辑,我创建了一个变量 spawnOffset 并且每帧递增 +1。
if spawnOffset % 4 == 0 改变方向
但是,我没有定期改变方向,当我调试时,跳帧,逻辑也是如此
public GameObject go;
public Transform Playertransform;
public Vector3 tileSpawnOffset = Vector3.zero;
private Vector3 direction = Vector3.forward;
public int SpawnOffset = -3;
private bool turnRight = false;
void Update()
{
SpawnPath();
ChangeDirection();
}
void SpawnPath()
{
if (Playertransform.position.z > SpawnOffset)
{
tileSpawnOffset += direction;
Instantiate(go, this.transform.position + tileSpawnOffset, this.transform.rotation, this.transform);
SpawnOffset++;
}
}
void ChangeDirection()
{
if (SpawnOffset % 4 == 0)
{
turnRight = !turnRight;
}
direction = turnRight == true ? Vector3.right : Vector3.forward;
}
float time = 0f;
void ChangeDirection()
{
time += Time.deltaTime;
if (time > 1)
{
turnRight = !turnRight;
time = 0;
}
direction = turnRight == true ? Vector3.right : Vector3.forward;
}
它工作得很好。所以,我该如何解决它。我,不想使用时间,我想在生成 4 个瓷砖后恰好改变方向
因为你总是同时调用 SpawnPath();改变方向(); everyframe if (Playertransform.position.z > SpawnOffset) is ever false in false twice your turnright bool gets flipped regardless every frame you on a multiple of 4. 如果你没有生成新的图块,则不需要调用 ChangeDirection .如果您只是从更新中删除该调用并在增量 SpawnOffset 之后立即添加它,它可能会修复它。