【发布时间】:2020-04-12 10:31:46
【问题描述】:
我正在做一扇门(类似于 Doom 64 的门),我有这个代码:
public class aperturaPorta : MonoBehaviour
{
public Transform playerCheck;
public Vector3 halfSize = new Vector3 (3f, 3f, 0.5f);
public LayerMask playerLayer;
bool playerEntering = false;
public BoxCollider collider;
public MeshRenderer renderer;
bool aprendo = false;
bool chiudendo = false;
public float openSpeed;
int counter = 1;
public int tick = 99;
// Update is called once per frame
void Update()
{
playerEntering = Physics.CheckBox(playerCheck.position, halfSize, Quaternion.identity, playerLayer, QueryTriggerInteraction.UseGlobal);
if (playerEntering && Input.GetButtonDown("e")) {
aprendo = true;
chiudendo = false;
}
if (counter == 100) {
chiudendo = true;
}
if (aprendo) {
transform.position += new Vector3 (0f, openSpeed, 0f);
counter += 1;
if (counter > tick) {
aprendo = false;
chiudendo = true;
}
}
if (chiudendo) {
transform.position -= new Vector3 (0f, openSpeed, 0f);
counter -= 1;
if (counter < 1) {
chiudendo = false;
}
}
}
}
这项工作,但门在打开后开始关闭,但速度太快,所以我想实现一个两三秒秒表,以便当它完成门开始关闭时,我该怎么做?谢谢
ps:对不起,我是团结的新手
【问题讨论】:
-
如果你想延迟这个过程,你可能不得不使用 thread.sleep ,每次渲染更新后大约 1 秒
-
@Saravanan 对不起,但这似乎不起作用,如果我使用它,它会“冻结”一切两秒钟,我什至无法移动我的播放器..这是正常的还是我用错了吗?
-
你可以试试下面的异步版本
await Task.Delay(1000);
标签: c# unity3d timer stopwatch