长按0.3秒怎么重置分数

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

我想知道如果你按住 3 秒你的分数会重置。我尝试了一个 if 语句,但没有用,因为它是统一的,所以我想帮助我,我是一个新的编码员,我不太了解。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class GameManager : MonoBehaviour
{
    public int clicks = 0;
    public TMP_Text clickText;



    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        clickText.text = "Clicks: " + clicks;
    }

    public void Click() {
        clicks++;
    }
}

unity3d
2个回答
0
投票

您可以使用

Input.GetKey()
这将帮助您检查何时按下键。 将其添加到您的
Update()
方法中。


0
投票

要在按下按钮 3 秒后重置,请尝试以下示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class GameManager : MonoBehaviour
{
    public int clicks = 0;
    public TMP_Text clickText;
    pubic float timeHeld;


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        myButton = </Insert Get Input Method>
        clickText.text = "Clicks: " + clicks;
        if (myButton) {
            timeHeld += Time.deltaTime;
        } else {
            timeHeld = 0;
        }
        if (timeHeld > 3) {
            clicks = 0;
        }
    } 

    public void Click() {
        clicks++;
    }
}

细分:

  1. timeHeld 增加 Time.deltaTime 当按下按钮时,如果没有重置,这意味着在 3 秒内按钮 bool 为真 timeHeld 将为 3.0f。

  2. 我们检查timeHeld是否大于重置clicks所需的时间,如果是则重置它。

如果您使用的是新的输入系统,只需在输入操作中添加一个保持interaction,以在保持该时间时触发它

© www.soinside.com 2019 - 2024. All rights reserved.