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

Unity 2D平台游戏:角色掉落方块时仍可跳跃的问题

Unity 2D平台角色掉落时误触发跳跃的二段跳问题

问题描述

正在开发一款Unity 2D平台游戏,实现了二段跳功能,但遇到问题:角色从方块上掉落时,无需落地就能发起一次跳跃,不符合预期。已尝试缩小地面检测半径,确认jumpableGround图层设置正确,但问题仍未解决。

相关代码

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

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody2D rb;
    private float dirX = 0f;
    [SerializeField] private float moveSpeed = 7f;
    [SerializeField] private float jumpForce = 14f;
    private bool doubleJump = false;
    private BoxCollider2D coll;
    [SerializeField] private LayerMask jumpableGround;
    //[SerializeField] private AudioSource jumpSoundEffect;
    public KeyCode left;
    public KeyCode right;
    public KeyCode jump;
    public Transform groundCheck;
    public float groundCheckRadius;


    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<BoxCollider2D>();
    }

    private void Update()
    {
        HandleMovement();
        HandleJump();
    }

    private void HandleMovement()
    {
        if (Input.GetKey(left))
        {
            dirX = -1f;
        }
        else if (Input.GetKey(right))
        {
            dirX = 1f;
        }
        else
        {
            dirX = 0f;
        }

        rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
    }
    
    private void HandleJump()
    {
        if (IsGrounded())
        {
            doubleJump = false;
        }

        if (Input.GetKeyDown(jump))
        {
            if (IsGrounded() || !doubleJump)
            {
                doubleJump = !IsGrounded();
                rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            }
        }
        else if (Input.GetKeyUp(jump) && rb.velocity.y > 0f)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
        }
    }

    private bool IsGrounded()
    {
        bool grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, jumpableGround);
        return grounded;
    }
}

问题根源

当前跳跃逻辑存在漏洞:角色掉落时doubleJump保持false状态,而跳跃条件IsGrounded() || !doubleJump会被触发(!doubleJump为true),导致空中无授权的跳跃被允许。需要调整逻辑,仅当角色在地面发起第一次跳跃后,才激活二段跳权限。

修复方案

修改HandleJump方法的逻辑,明确区分地面跳跃和二段跳的触发条件:

private void HandleJump()
{
    // 落地时重置二段跳权限
    if (IsGrounded())
    {
        doubleJump = false;
    }

    if (Input.GetKeyDown(jump))
    {
        // 地面跳跃:发起跳跃并激活二段跳权限
        if (IsGrounded())
        {
            doubleJump = true;
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
        // 二段跳:仅当有未使用的二段跳权限时触发
        else if (doubleJump)
        {
            doubleJump = false;
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }
    else if (Input.GetKeyUp(jump) && rb.velocity.y > 0f)
    {
        rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    }
}

修复说明

  • 角色落地时,doubleJump被重置为false,表示尚未激活二段跳权限
  • 仅在地面按下跳跃键时,才将doubleJump设为true,授予二段跳权限
  • 空中只有在doubleJump为true时才能触发二段跳,同时消耗该权限
  • 角色掉落时,由于未在地面发起跳跃,doubleJump保持false,空中按下跳跃键不会触发任何跳跃

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 21:07:20