简体   繁体   English

Unity2D如何限制玩家跳跃

[英]Unity2D how to limit player jump

So im making a clicker game and here is my code.所以我正在制作一个点击游戏,这是我的代码。 What i want to ask is how to limiting button to click so it can't be clicked by multiple time, because if i clicked it multiple time the speed became too fast我想问的是如何限制按钮点击,所以它不能被多次点击,因为如果我多次点击它,速度就会变得太快

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;

this to get the Button reference and specify the duration获取 Button 引用并指定持续时间

coroutine = ButtonDelayed(btnDelay);
StartCoroutine(coroutine);

this after you call Jump();这在你调用Jump();之后in your update or in Jump()在您的更新或Jump()

IEnumerator ButtonDelayed(float delay)
{
BTN.interactable = false;
yield return new WaitForSeconds(delay);
BTN.interactable = !BTN.interactable;
}

this somewhere.这个地方。 Just a quick mockup.只是一个快速的模型。 Not sure if you will get an exception.不知道你是否会得到一个例外。 If you have a problem just hit me up.如果你有问题就打我。

EDIT: I forgot to tell you to change the color of the disabled state in the inspector to the color you have when the Button is interactable.编辑:我忘了告诉你将检查器中禁用的 state 的颜色更改为按钮可交互时的颜色。 Otherwise you will see the Button change colors.否则,您将看到 Button 更改 colors。

EDIT2: Full script updated 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;
}

I still can't reply to comments so I'll post it here我仍然无法回复评论,所以我会在这里发布

To use IEnumerator is just like any other function使用 IEnumerator 就像任何其他 function

you put it somewhere in your code and then call it the difference is when you call it you add你把它放在你的代码中的某个地方然后调用它不同的是当你调用它时你添加

StartCoroutine(MethodName());启动协程(方法名());

and it will run the first part of the code - then Wait For the amount of time you specified, then it will run the second part of the code它将运行代码的第一部分 - 然后等待您指定的时间量,然后它将运行代码的第二部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM