【问题标题】:Stopwatch in c# (unity)c#中的秒表(统一)
【发布时间】: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


【解决方案1】:

如果我理解正确,您希望在门打开之前在门关闭之前进行简单的延迟?保持相同的代码结构,您可以为此添加其他计数器。

public float openSpeed;
int counter = 1;
public int tick = 99;
public int delayTicks = 100;
private int delayCounter = 0;

// Update is called once per frame
void Update()
{
    playerEntering = Physics.CheckBox(playerCheck.position, halfSize, Quaternion.identity, playerLayer, QueryTriggerInteraction.UseGlobal);

    if (playerEntering && Input.GetKeyDown(KeyCode.P))
    {
        aprendo = true;
        chiudendo = false;
    }

    if (counter == 100)
    {
        chiudendo = true;
    }

    if (aprendo)
    {
        transform.position += new Vector3(0f, openSpeed, 0f);
        counter += 1;
        if (counter > tick)
        {
            delayCounter = delayTicks;
            aprendo = false;
        }
    }

    if (delayCounter > 0) 
    {
        delayCounter--;
        if (delayCounter <= 0)
        {
            chiudendo = true;
        }
    }
    else if (chiudendo)
    {
        transform.position -= new Vector3(0f, openSpeed, 0f);
        counter -= 1;
        if (counter < 1)
        {
            chiudendo = false;
        }
    }
}

【讨论】:

  • 我建议您考虑在 Update 中为您的计时器使用实际时间,例如 Time.deltaTime。这个answers.unity.com/questions/351420/simple-timer-1.html有很多好帖子@
  • 我看到了你链接的帖子,我添加了一个浮点数“ritardoChiusura = 2f”(2f 应该对应于 2 秒),如果 ritardoChiusura 大于 0,则减去 Time.deltaTime,当它小于 0 则开始关门。谢谢,这似乎有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-05
  • 1970-01-01
  • 2021-07-27
  • 2011-05-05
相关资源
最近更新 更多