所以我正在制作一个点击游戏,这是我的代码。我想问的是如何限制按钮的单击,以便不能多次单击它,因为如果我多次单击它,速度会变得太快
public float downForce;
public float speed;
public int playerHp;
public Text healthText;
Rigidbody2D rb;
CharacterController controller;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
healthText.text = playerHp.ToString();
if (Input.GetMouseButtonDown(0))
{
Jump();
}
if (playerHp < 0)
{
Destroy(this.gameObject);
SceneManager.LoadScene("GameOver");
}
}
public void Jump()
{
rb.AddForce(Vector2.up * downForce + Vector2.right * speed, ForceMode2D.Impulse);
rb.isKinematic = false;
}
public Button BTN;
public float btnDelay = .5f;
获取按钮参考并指定持续时间
coroutine = ButtonDelayed(btnDelay);
StartCoroutine(coroutine);
调用Jump();
更新或之后Jump()
IEnumerator ButtonDelayed(float delay)
{
BTN.interactable = false;
yield return new WaitForSeconds(delay);
BTN.interactable = !BTN.interactable;
}
这个地方。只是一个快速的模型。不知道您是否会得到例外。如果您有问题,请打我。
编辑:我忘了告诉你将检查器中禁用状态的颜色更改为按钮可交互时的颜色。否则,您将看到按钮更改颜色。
EDIT2:已更新完整脚本
public float downForce;
public float speed;
public int playerHp;
public Text healthText;
Rigidbody2D rb;
CharacterController controller;
public Button BTN;
public float btnDelay = .5f;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
healthText.text = playerHp.ToString();
if (Input.GetMouseButtonDown(0))
{
Jump();
}
if (playerHp < 0)
{
Destroy(this.gameObject);
SceneManager.LoadScene("GameOver");
}
}
public void Jump()
{
coroutine = ButtonDelayed(btnDelay);
StartCoroutine(coroutine);
rb.AddForce(Vector2.up * downForce + Vector2.right * speed,
ForceMode2D.Impulse);
rb.isKinematic = false;
}
IEnumerator ButtonDelayed(float delay)
{
BTN.interactable = false;
yield return new WaitForSeconds(delay);
BTN.interactable = !BTN.interactable;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句