我在unity 3D中的脚本有问题。我正在尝试制作一个可收集的系统,当我收集物品时,游戏会自动启动慢动作,但只有3秒,然后它就会恢复正常。当我收集第一件物品时,我可以做到,但当我收集第二件物品时,它不会回到正常的速度。在我收集了第二件物品后,我的Time.fixedTime在4秒内被卡住了…它不再更新。
using UnityEngine;
public class CollectableDestroy : MonoBehaviour
{
public GameObject gb;
public float startTime;
private float realTime;
private void FixedUpdate() {
realTime = Time.fixedTime; // Stores game's time
if(realTime - startTime >= 3){ // If has passed 3 seconds after hitting the trigger, if returns to normal speed
PlayerMov.velocityObject = 50f;
}
Debug.Log(realTime);
}
private void OnTriggerEnter() {
startTime = Time.fixedTime; // It stores time when player hit trigger
GameObject.Destroy(gb);
PlayerMov.velocityObject = 1f; // It turns in slowmotion
}
}发布于 2019-09-14 04:45:18
我不知道这是否是问题所在,但是您可以简单地将OnTriggerEnter定义为Coroutine并使用WaitForSeconds和WaitForFixedUpdate
bool alreadySlowDone;
private IEnumerator OnTriggerEnter()
{
// in case you call this multiple times wait until one routine finished
// here you can decide what you want to do .. I would block further triggers until bac to normal
if(alreadySlowDone) yield break;
alreadySlowDone = true;
// still questionable why you do this here but maybe at least check it
if(gb) GameObject.Destroy(gb);
else Debug.LogError("Why do I try to destroy something here that doesn't exist?", this);
PlayerMov.velocityObject = 1f; // It turns in slowmotion
// wait for 3 seconds
yield return new WaitForSeconds(3f);
// wait for the next fixed update
yield return new WaitForFixedUpdate();
PlayerMov.velocityObject = 50f;
alreadySlowDone = false;
}发布于 2019-09-14 04:25:23
与Time.fixedTime一起工作非常奇怪。处理能力最终成为如何返回时间的问题。它可能会以一种在每次激活触发器时都会给出不同值的方式来解除同步。导致你的问题。
为什么不使用Time.deltaTime (更稳定),或者让触发器OnExitTrigger重置realTime ,或者在FixedUpdate() if语句中使用,以确保故障安全,以防万一
https://stackoverflow.com/questions/57929638
复制相似问题