我想知道如果你按住 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++;
}
}
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++;
}
}
timeHeld 增加 Time.deltaTime 当按下按钮时,如果没有重置,这意味着在 3 秒内按钮 bool 为真 timeHeld 将为 3.0f。
我们检查timeHeld是否大于重置clicks所需的时间,如果是则重置它。
如果您使用的是新的输入系统,只需在输入操作中添加一个保持interaction,以在保持该时间时触发它。