C#中的秒表(统一)

问题描述 投票:0回答:1

我正在开门(类似于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:对不起,但我是一个团结的新手

c# unity3d timer stopwatch
1个回答
0
投票

如果我理解正确,您想在门打开之前先简单延迟一下,然后再关闭吗?保持相同的代码结构,您可以为此添加另一个计数器。

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;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.