You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity C# 2D平台跳跃游戏二段跳异常问题求助

2D平台跳跃游戏二段跳BUG求助(已实现Coyote Time)

我在2D平台跳跃游戏中实现了Coyote Time(郊狼时间),但跳跃功能出现严重BUG:首次跳跃正常,二段跳时而有效时而无效,无法定位问题。

以下是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    private Rigidbody2D rb;
    private PolygonCollider2D coll;


    private float horizontal;

    [SerializeField] private LayerMask jumpableGround;
    [SerializeField] private float MoveSpeed = 5f;
    [SerializeField] private float JumpForce = 5f;


    private bool CanDash = true;
    private bool IsDashing;

    [SerializeField] private float DashPower = 5f;
    [SerializeField] private float DashCooldown = 1f;
    [SerializeField] private float DashTime = 0.4f;

    [SerializeField] private float CoyoteTime = 0.2f;
    private float CoyoteTimeCounter;

    private bool DoubleJump; 



    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<PolygonCollider2D>();
        CanDash = true;
    }

    void Update()
    {

        if (IsGrounded())
        {
            CoyoteTimeCounter = CoyoteTime;
        }
        else
        {
            CoyoteTimeCounter -= Time.deltaTime;
        }


        horizontal = Input.GetAxisRaw("Horizontal");



        if (CoyoteTimeCounter > 0f && !Input.GetKey("space"))
        {
            DoubleJump = false;
        }

        if (Input.GetKeyDown("space"))
        {

            if (CoyoteTimeCounter > 0f || DoubleJump)
            {
                rb.velocity = new Vector2(rb.velocity.x, JumpForce);

                DoubleJump = !DoubleJump;

            }
            
        }



        if (Input.GetKeyUp("space") && rb.velocity.y > 0f)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);


        }

        if (Input.GetKeyDown(KeyCode.LeftShift) && CanDash)
        {
            StartCoroutine(Dash());
        }




    }


    private void FixedUpdate()
    {
        if (IsDashing)
        {
            return;
        }

        rb.velocity = new Vector2(horizontal * MoveSpeed, rb.velocity.y);
    }

    private IEnumerator Dash()
    {
        CanDash = false;
        IsDashing = true;
        float OriginalGravity = rb.gravityScale;
        rb.gravityScale = 0f;
        rb.velocity = new Vector2(horizontal * DashPower, 0f);
        yield return new WaitForSeconds(DashTime);
        rb.gravityScale = OriginalGravity;
        IsDashing = false;
        yield return new WaitForSeconds(DashCooldown);
        CanDash = true;
    }

    private bool IsGrounded()
    {
        return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
    }

}

P.S. 我知道自己的代码写得不好,我是C#新手,任何反馈都有帮助!

内容的提问来源于stack exchange,提问作者evanmccaig

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 11:08:30