为什么我的角色在跳跃时失去了水平速度?

Why is my character's horizontal velocity lost when jumping?

目标
我对 Unity 比较陌生,我希望我的角色能够 运行 并同时跳跃,使角色沿对角线上升。

问题
然而,在进行了一些调整以赋予角色一定的加速度之后,跳跃似乎清除了所有现有的速度,这意味着角色上升然后向侧面而不是同时上升:

(如果有点难看请见谅)

代码
这是我的角色移动脚本:

    Rigidbody2D rb;
    BoxCollider2D bc;   

    [Header("Run")]
    float xInput = 0f;
    public float maxRunSpeed;
    public float acceleration;
    [Space]

    [Header("Jump")]
    public float jumpHeight;
    public float lowJumpHeight;
    public float fallSpeed;
    public float airControl;

    [Space]
    public LayerMask groundLayer;
    public bool onGround;

    [Space]
    public Vector2 bottomOffset;
    public Vector2 boxSize;
    public float coyoteTime;

    void Start() {
        // Gets a reference to the components attatched to the player
        rb = GetComponent<Rigidbody2D>();
        bc = GetComponent<BoxCollider2D>();
    }

    void Update() {
        Jump();

        // Takes input for running and returns a value from 1 (right) to -1 (left)
        xInput =  Math.Sign(Input.GetAxisRaw("Horizontal"));
    }

    // Applies a velocity scaled by runSpeed to the player depending on the direction of the input
    // Increaces the velocity by accerleration until the max velocity is reached
    void FixedUpdate() {
        rb.velocity = Math.Abs(rb.velocity.x) < Math.Abs(xInput) * maxRunSpeed ? rb.velocity + new Vector2(acceleration * xInput, rb.velocity.y) * Time.deltaTime : new Vector2(xInput * maxRunSpeed, rb.velocity.y);   
    }

    void Jump() {
        // Checks whether the player is on the ground and if it is, replenishes coyote time, but if not, it starts to tick it down
        coyoteTime = onGround ?  0.1f :  coyoteTime - Time.deltaTime;
        // Draws a box to check whether the player is touching objects on the ground layer
        onGround = Physics2D.OverlapBox((Vector2)transform.position + bottomOffset, boxSize, 0f, groundLayer);

        // Adds an upwards velocity to player when there is still valid coyote time and the jump button is pressed
        if (Input.GetButtonDown("Jump") && coyoteTime > 0) {
            rb.velocity = Vector2.up * jumpHeight;
        }
        
        // Increases gravity of player when falling down or when the jump button is let go mid-jump
        if (rb.velocity.y < 0 ) {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (fallSpeed - 1) * Time.deltaTime;
        } else if (rb.velocity.y > 0 && !Input.GetButton("Jump")) {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpHeight - 1) * Time.deltaTime;
        }
    }

很抱歉有很多不必要的代码,只是我不确定是什么导致了这个问题,所以我不想删除任何东西。希望我的评论有意义吗?

发生这种情况是因为您直接使用 rb.velocity = Vector2.up * jumpHeight 设置刚体的速度。所以这将消除所有现有的速度。

如果您只想向速度添加一个力而不是完全替换它,您可以使用 Rigidbody2D.AddForce.

等方法来实现

虽然在所有其他情况下你保留你的速度值并且只稍微修改它们你很难覆盖

中的绝对值 velocity
rb.velocity = Vector2.up * jumpHeight;

正在删除 X 上的任何速度。

你可以简单地保留你在另一个轴上的任何东西,只覆盖 Y 就像

var velocity = rb.velocity;
velocity.y = jumpHeight;
rb.velocity = velocity;

rb.velocity = new Vector2(rb.velocity.x, jumpHeight);

水平方向的 FixedUpdate 基本上和你做的一样。