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

Unity 2D游戏C#玩家冲刺问题:静止状态下无法触发冲刺

问题原因

你的冲刺功能在静止时失效,核心原因是冲刺时依赖的horizontal变量在无方向输入时为0:
在Dash()协程中,你设置rb.velocity = new Vector2(horizontal * DashPower, 0f),当玩家没按左右键时,Input.GetAxisRaw("Horizontal")返回0,导致x轴速度为0,自然没有冲刺位移。

修复方案

添加一个变量记录玩家最后一次的移动方向,静止时用这个方向执行冲刺。具体修改步骤如下:

  1. 添加变量存储最后移动方向:
private float lastHorizontalDirection = 1f; // 默认向右,可按需调整
  1. 在Update()中更新这个变量,只要玩家有方向输入就记录:
horizontal = Input.GetAxisRaw("Horizontal");
// 记录最后移动方向
if (horizontal != 0)
{
    lastHorizontalDirection = horizontal;
}
  1. 修改Dash()协程中的速度赋值逻辑,无当前方向输入时使用记录的历史方向:
// 优先用当前输入方向,无输入则用最后记录的方向
float dashDirection = horizontal != 0 ? horizontal : lastHorizontalDirection;
rb.velocity = new Vector2(dashDirection * DashPower, 0f);
修改后的完整代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody2D rb;
    private BoxCollider2D coll;

    private float horizontal;
    private float lastHorizontalDirection = 1f; // 记录最后移动方向,默认向右

    [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;

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

    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");

        // 更新最后移动方向
        if (horizontal != 0)
        {
            lastHorizontalDirection = horizontal;
        }

        if (Input.GetKeyDown("space") && IsGrounded())
        {
            rb.velocity = new Vector2(rb.velocity.x, JumpForce);
        }

        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;

        // 确定冲刺方向:当前有输入则用输入,无输入则用最后记录的方向
        float dashDirection = horizontal != 0 ? horizontal : lastHorizontalDirection;
        rb.velocity = new Vector2(dashDirection * 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);
    }
}
额外建议
  • 如果你的玩家有Sprite翻转逻辑,可以把lastHorizontalDirection和Sprite朝向绑定,让冲刺方向和玩家视觉方向一致;
  • 可以给静止冲刺添加地面判断(复用IsGrounded()),避免空中静止冲刺的不合理性。

内容的提问来源于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 10:25:53