簡體   English   中英

c# 中的秒表(統一)

[英]Stopwatch in c# (unity)

我正在做一扇門(類似於 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:對不起,我是團結的新手

如果我理解正確,您想要在門打開之前簡單延遲一下嗎? 保持相同的代碼結構,您可以為此添加其他計數器。

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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM