玩家跳跃高度不一致 - Unity

Player Jumping Height is not consistent - Unity

我正在开发一款 2D 平台游戏,我意识到玩家的跳跃功能并不是每次都以相同的方式工作,例如。如果玩家在 moving/running 时跳跃或玩家跳跃而不移动,则跳跃高度不同。

我有 2 个独立的函数 Move() 和 Jump(),Move() 使用 transform.Translate 让玩家移动,而 Jump() 使用 rigidBody.AddForce() 让玩家跳跃.我已经尝试更改玩家 Move() 函数以使用 rigidBodies 让玩家移动而不是使用 transform.Translate()。而且它没有用。

我也尝试过使用 transform.Translate 让玩家跳跃,这解决了跳跃高度不一致的问题,但玩家只是向上传送而不是跳跃

这是我的代码结构的表示,而不是实际代码,因为实际代码大约有 600 行

public class Player
{
    float JumpSpeed;
    bool isGrounded;

    void Update()
    {
        if (Input.GetKey(KeyCode.A))
            Move(Directions.Left);

        if (Input.GetKey(KeyCode.D))
            Move(Directions.Right);

        if (Input.GetKeyDown(KeyCode.Space))
            Jump(JumpSpeed);
    }

    public void Move(Directions dir)
    {
        Vector2 speed;
        //figure out speed and etc...

        //makes the player move in the right direction and speed
        transform.Translate(speed * Time.deltaTime);
    }

    public void Jump(float speed)
    {
        if(isGrounded)
            rigidBody.AddForce(new Vector2(0, speed * Time.deltaTime), ForceMode2D.Impulse);
    }
}

不确定这是否是您的问题,但使用平移移动玩家并然后 增加跳跃力并不是解决问题的最佳方法。

我会考虑将刚体的速度部分用于跳跃和运动。这将防止翻译对象可能导致的任何怪异。