一段时间后生成对象会根据时间尺度产生不同的结果
Posted
技术标签:
【中文标题】一段时间后生成对象会根据时间尺度产生不同的结果【英文标题】:Spawning objects after a period of time results in different outcome depending on time scale 【发布时间】:2018-04-17 11:43:39 【问题描述】:这是我脚本的一部分。 GameObjects 不会在我期望的时间产生。我试图改变与延迟值相关的时间尺度(即 timescale = 1 & baseDelay = .1f 到 timescale = 10 & baseDelay = 1)。这就像一个魅力,我真的不知道为什么。我的代码有问题吗? FixedUpdate 和小浮点数有统一问题吗?
图片:
http://i.epvpimg.com/hwgsbab.png http://i.epvpimg.com/WYBfbab.pngusing System; using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
internal int xCount = 5; //bricks in x per line
internal float spacing = .5f; //space between bricks and margin to edges
internal float baseDelay = .1f; //time that needs to pass until the next movement internal
float brickMovementPerStep = .05f; //movement distance per step
int currentLineNumber = 0; //index for current line
void FixedUpdate()
//accurate value of space which needs to pass
float dist = ((screenSize.x - (2 * spacing + ((xCount - 1) * spacing))) / xCount) * .667f + spacing;
float stepsPerSecond = 1 / baseDelay; //how many steps are there per second?
float movementPerSecond = stepsPerSecond * brickMovementPerStep; //how far will a line have moved?
float requiredTime = dist / movementPerSecond; //how long will a line need to travel until the next one can be spawned?
timeSinceLastSpawn += Time.deltaTime;
if(timeSinceLastSpawn >= requiredTime)
timeSinceLastSpawn = 0; //reset time
currentLineNumber++;
SpawnAndStartLevel(); //this instantiates and moves the line, it is moved continously
【问题讨论】:
FixedUpdate
应该用于物理操作。您可能想改用Update
。如果您仍想使用FixedUpdate
,那么请使用Time.fixedDeltaTime
而不是Time.deltaTime
【参考方案1】:
Time.deltaTime
用于更新每一帧的函数。 FixedUpdate()
并非每帧都运行。 FixedUpdate()
用于物理 - 因为你没有做任何物理,你应该将它重命名为 Update()
。我敢打赌,这将解决您的计时异常。 Read More
【讨论】:
【参考方案2】:肯定有可能发生舍入错误。我建议为此使用带有yield return WaitForSeconds(float)
的协程,因为您似乎在固定的时间间隔内做事。
internal int xCount = 5; //bricks in x per line
internal float spacing = .5f; //space between bricks and margin to edges
internal float baseDelay = .1f; //time that needs to pass until the next movement internal
float brickMovementPerStep = .05f; //movement distance per step
int currentLineNumber = 0; //index for current line
bool run = true;
bool reset = false;
void Start()
StartCoroutine(LineRoutine(baseDelay));
public IEnumerator LineRoutine(float delay)
while (run)
float dist = ((screenSize.x - (2 * spacing + ((xCount - 1) * spacing))) / xCount) * .667f + spacing;
float stepsPerSecond = 1 / delay; //how many steps are there per second?
float movementPerSecond = stepsPerSecond * brickMovementPerStep; //how far will a line have moved?
float requiredTime = dist / movementPerSecond; //how long will a line need to travel until the next one can be spawned?
while (!reset)
// stuff to happen every [requredTime] seconds
yield return new WaitForSeconds(requiredTime);
currentLineNumber++;
SpawnAndStartLevel();
reset = false;
顺便说一句,您在 FixedUpdate 中使用 Time.deltaTime 还是 Time.fixedDeltaTime 都没有关系,如 unity docs 中所述
请注意,我不会重新计算每次迭代所需的时间,因为在我看来它不会不断变化。但是,如果您将 reset
设置为 true,它将离开该循环并重新计算所需的时间。
【讨论】:
我更改了 Time.* 和 FixedUpdate,因此它在项目中的所有脚本中都是统一的。在编辑了一些代码(特别是删除了一些我没有包含在上面的代码中的过时的东西)之后,它又可以工作了。感谢您抽出宝贵时间,感谢您的帮助!以上是关于一段时间后生成对象会根据时间尺度产生不同的结果的主要内容,如果未能解决你的问题,请参考以下文章
直接访问与在开发工具中读取对象时访问 javascript 属性会产生不同的结果