按键冷却 [英] Key press cooldown

查看:30
本文介绍了按键冷却的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事水平 2D 无限跑步者的个人项目.我在跳跃机制方面遇到了问题,玩家可以按住跳跃按钮并在他触地时立即跳跃.我想强制玩家释放按钮才能再次跳跃.我想在他漂浮时制作相同的机制(在跳跃结束时,当玩家开始下降时,其 y 速度会降低几秒).我遵循单一责任原则,因此跳跃和浮动是 2 个单独的脚本.

I am working on solo project that is horizontal 2D infinite runner. I have a problem in jumping mechanics where player can hold the jump button and jump as soon as he touches the ground. I want to force player to release the button to be able to jump again. I want to make same mechanics when he is floating(at the end of the jump when player starts falling down its y velocity is being reduced for few bits of second). I am following single responsibility principle so jumping and floating are 2 seperate scripts.

我尝试实现一个计时器,它会在玩家接触地面时计数,一段时间后玩家可以再次跳跃,但没有得到询问结果,因为您可以一直按住跳跃按钮时间和在地面上花费的确定时间后,玩家将再次跳跃而不释放按钮.

I have tried to implement a timer that will count while player is touching the ground and after some time the player would be able to jump again, but didn't manage to get asked result because you can hold the jump button all the time and after determined time spent on ground player would just jump again without releasing the button.

public class Jumping : MonoBehaviour
{
    public bool isJumping;
    public bool isGrounded;
    public float speedUp;
    public float verticalAxis;

    public float timePassed;
    public float jumpLimit;
    private Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        verticalAxis = Input.GetAxisRaw("Vertical");
        if (verticalAxis > 0 && timePassed == 0)
        {
            isJumping = true;
        }
    }

    private void FixedUpdate()
    {
        if (isJumping)
        {
            Jump();
        }
    }

    private void OnCollisionStay2D(Collision2D collision)
    {
        if (collision.collider.tag == "Ground")
        {

            isGrounded = true;
            timePassed = 0f;
        }
    }


    private void Jump()
    {
        isGrounded = false;
        //Modifying y component of players rigidbody(jumping)
        Vector2 velocity = Vector2.up * speedUp * verticalAxis;
        rb.velocity = velocity;

        //Counting time when jumping 
        timePassed += Time.deltaTime;
        if (timePassed >= jumpLimit)
        {
            isJumping = false;
        }
    }
}

附言我试图按照stackoverflow的建议缩小这个问题中的代码,但我不知道要删掉什么,因为据我说,一切都与解决问题相关且至关重要.谢谢!

P.S. I have tried to shrink the code in this question as stackoverflow suggests but I didn't know what to cut out because according to me everything is relevant and crucial to solving the problem. Thanks!

推荐答案

在不知道你的其余代码的情况下,我会使用一个额外的标志,显然是为了获得像

without knowing the rest of your code I would use an additional flag and obviously something for getting the cool down delay like

public float jumpCoolDownTime;
private float coolDownTimer;
private bool canJump;

void Update()
{
    // if no can jump you can't jump again
    if(!canJump)
    {
        verticalAxis = Input.GetAxisRaw("Vertical");
        if (verticalAxis > 0)
        {
            canJump = false;
            isJumping = true;
            coolDownTimer = 0;
        }
    }
    else
    {
        // run timer to enable jump again
        if(isGrounded) coolDownTimer += Time.deltaTime;

        if(coolDownTimer >= jumpCoolDownTime)
        {
            canJump = true;
        }
    }
}

<小时>

不过,您可以使用 协程

public bool isJumping;
public float speedUp;
public float verticalAxis;

public float jumpLimit;
public float coolDownTime;

private Rigidbody2D rb;

// Start is called before the first frame update
void Start()
{
    rb = gameObject.GetComponent<Rigidbody2D>();
}

void Update()
{
    // do nothing is already jumping
    if(isJumping) return;

    verticalAxis = Input.GetAxisRaw("Vertical");
    if (verticalAxis > 0)
    {
        // start jumping
        StartCoroutine(Jump());
    }
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.collider.tag != "Ground") return;

    isGrounded = true;
    StartCoroutine(CoolDown());
}

private IEnumerator Jump()
{
    isJumping = true;
    isGrounded = false;
    coolDownTimer = 0;

    var timePassed = 0f;
    while(timePassed < jumpLimit)
    {
        // wait for the FixedUpdate call
        yield return new WaitForFixedUpdate();

        Vector2 velocity = Vector2.up * speedUp * verticalAxis;
        rb.velocity = velocity;
    }
}

private IEnumerator CoolDown()
{
    yield return new WaitForSeoncds(coolDownTime);

    isJumping = false;
}

这篇关于按键冷却的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆