为什么我按住跳跃输入,角色就一直往上走?

问题描述 投票:0回答:1

我之前写过一个可以工作的播放器控制器,一切都运行得很好。唯一的问题是我在一个脚本中做了太多事情,所以我决定通过将其拆分为几个可以相互通信的较小脚本来清理它。

我现在有一个脚本接受玩家输入(使用新的输入系统),一个脚本管理玩家动作(移动、跳跃等),另一个脚本处理所有动画。

除了跳跃之外一切都工作正常。

我已经查看了旧脚本和新脚本,据我所知,一切都完全相同。然而,在旧脚本中,无论您按住按钮多长时间,玩家都会根据跳跃力跳跃设定的高度。使用新的脚本,只要按住按钮,玩家就会不断上升。

这是原始脚本的跳转函数:


    public void OnJump(InputAction.CallbackContext context)
    {
        bool jumpButtonPressed = context.performed;
        bool jumpButtonReleased = context.canceled;
        
        if (jumpButtonPressed && coyoteTimeCounter > 0f)
        {
            rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
        }

        if (jumpButtonReleased && rb.velocity.y > 0f)
        {
            rb.velocity = new Vector2 (rb.velocity.x, rb.velocity.y * 0.5f);
            coyoteTimeCounter = 0f;
        }

这是新脚本中的跳转函数以及输入脚本中的输入函数

public class PlayerInputs : MonoBehaviour
{
    public bool jumpInputPressed;
    public bool jumpInputReleased;

    public void OnJump(InputAction.CallbackContext context)
    {
        jumpInputPressed = context.performed;
        jumpInputReleased = context.canceled;
    }
}
public class PlayerController : MonoBehaviour
{
    void Update() 
    {
        Jumping();
    }

    void Jumping()
    {
        if (_pInputs.jumpInputPressed && coyoteTimeCounter > 0f)
        {
            rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
        }

        if (_pInputs.jumpInputReleased && rb.velocity.y > 0f)
        {
            rb.velocity = new Vector2 (rb.velocity.x, rb.velocity.y * 0.5f);
            coyoteTimeCounter = 0f;
        }
    }

我不知道这两者的工作方式有何不同。

c# unity-game-engine controller
1个回答
0
投票

因此,在我的原始脚本中,我有更新方法在整个脚本中调用其他方法。我没有注意到 Jump 方法不在其中,这就是为什么我认为没有什么不同。

© www.soinside.com 2019 - 2024. All rights reserved.