【问题标题】:JumpCount resets immediately after jumping 2D platformerJumpCount 在跳跃 2D 平台游戏后立即重置
【发布时间】:2020-04-08 07:02:20
【问题描述】:

我对这一切都很陌生,对于新手的错误,我深表歉意。我正在制作一个 2D 平台游戏 我正在通过 RayCast 检查地面,在玩家跳跃后,jumpCounter 重置为 0,所以我得到了奖励跳跃。我尝试在重置之前添加一个 0.2 秒的计时器,但这会在连续多次跳跃(兔子跳跃)时造成麻烦。有什么帮助吗?

private void FixedUpdate()
    {
        GroundCheck();
        if (state != State.hurt)
        {
            Movement();
            DoubleJump();
            StateMachine();
        }

        HurtCheck();
        anim.SetInteger("state", (int)state);
    }
    private void Movement()
    {
        //Input.GetAxis returns a value between -1 up to 1
        //Edit> Project Settings> Input> Axis> Horizontal
        float hDirection = Input.GetAxisRaw("Horizontal");

        //holding down "D" makes the value positive and vice versa
        if (hDirection < 0)
        {
            rb.velocity = new Vector2(-speed, rb.velocity.y);
            transform.localScale = new Vector2(-1, 1);
        }
        else if (hDirection > 0)
        {
            rb.velocity = new Vector2(speed, rb.velocity.y);
            transform.localScale = new Vector2(1, 1);
        }
        else
        {

        }

        if (Input.GetButtonDown("Jump") && isGrounded == true)
        {
            Jump();
        }
    }
    private void Jump()
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpforce);
        state = State.jumping;
        jumpCount += 1;
    }
    private void GroundCheck()
    {
        Debug.DrawRay(transform.position, Vector2.down * hitDistance, Color.green);
        RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, hitDistance, ground);
        if(hit.collider != null)
        {
            isGrounded = true;
            jumpCount = 0;
        }
        else
        {
            isGrounded = false;
        }
    }
    private void DoubleJump()
    {
        if (Input.GetButtonDown("Jump") && isGrounded == false && jumpCount < 2)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpforce);
            jumpCount += 1;
        }
    }

【问题讨论】:

  • HurtCheck 在做什么?
  • 我感觉Vector2.down * hitDistance的值太大了,并且射线在跳转后的第一帧到达平台,因此将其重置为0。尝试在脚本中使用OnCollisionEnter地板进行重置。
  • 这解决了我的问题。 Raycast 太大了,但我需要它来在斜坡上运行动画,所以我创建了一个不同的并将IsGrounded 移动到OnCollisionEnter2D。非常感谢您的帮助!

标签: c# unity3d


【解决方案1】:

嘿,您是否确定您的播放器的图层不是ground 图层或正确设置了ground 图层蒙版,或者raycasthitDistance 太高它应该非常低,例如0.1 或更少或者jumpCount 是否从01 开始尝试交替使用,或者可能从两个开始以失去额外的奖金跳跃...这些将是我想看的地方,祝你好运!

【讨论】:

  • 光线投射的hitDistance 相当高,只是因为我希望玩家站在斜坡上时它能够到达地面。
  • 降低 hitDistance 时是否按预期工作?对于斜率检测,最好使用 BoxCast 而不是 raycast youtube.com/watch?v=c3iEl5AwUF8 这是一个非常好的教程。祝你好运,如果我解决了你的问题,请给我打勾!
猜你喜欢
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多