统一代码不允许角色(2D,刚体)在空中转向。我怎样才能用这个脚本获得更多的空气控制?

Unity code not allowing character (2D, rigidbody) to turn direction in air. How can I get more air control with this script?

在 Unity 中为我的游戏编写 2D 运动代码时,我遇到了问题。以前我决定让角色不能在空中移动,但是我想改变它。我该怎么做?我希望角色能够在空中转向,但速度不同于“moveSpeed”。这是我第一次访问这个网站,所以如果我泄露了太多细节,我深表歉意。以下是移动和跳跃脚本:

步行脚本

      public bool canMove = true;

        [SerializeField] public Vector2 newVelocity;
        [SerializeField] public Rigidbody2D rb;
        [SerializeField] public float moveSpeed = 10f;
        [SerializeField] public Vector2 direction;
        [SerializeField] public float wallSlideSpeed;
        public float movementForceAir;
        public float airDragMultiplier;
        FlipChar flipc;
        GroundCheck gc;
        SlopeWalk sw;
        PlayerJump pj;
        WallChecker wc;
        DashAfterimage da;

        void Awake()
        {
            gc = GetComponent<GroundCheck>();
            sw = GetComponent<SlopeWalk>();
            rb = GetComponent<Rigidbody2D>();
            pj = GetComponent<PlayerJump>();
            wc = GetComponent<WallChecker>();
            da = GetComponent<DashAfterimage>();
        }

        void Start()
        {
            flipc = GetComponent<FlipChar>();

        }

        void Update()
        {


        }

        void FixedUpdate()
        {
            if (gc.onGround && canMove && !da.dashing)
            {
                rb.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, rb.velocity.y);
            }
            else if (!gc.onGround && !wc.isWallSliding && Input.GetAxis("Horizontal") != 0)
            {

                Vector2 forceToAdd = new Vector2(movementForceAir * Input.GetAxis("Horizontal"), 0);
                rb.AddForce(forceToAdd);

                if (Mathf.Abs(rb.velocity.x) > moveSpeed)
                {
                    rb.velocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"), rb.velocity.y);


                    //          }else if(!gc.onGround && !wc.isWallSliding && Input.GetAxis("Horizontal") == 0){
                    //           rb.velocity = new Vector2(rb.velocity.x , rb.velocity.y);
                }
            }


            if (wc.isWallSliding)
            {
                if (rb.velocity.y < wallSlideSpeed)
                {
                    rb.velocity = new Vector2(rb.velocity.x, -wallSlideSpeed);
                }
            }

        }


    }
}

跳转脚本

        [HideInInspector] public GroundCheck gc;
        [HideInInspector] public PlayerWalk pw;

        [SerializeField] private float jumpForce;
        [SerializeField] private float jumpTimeCounter;
        [SerializeField] private float jumpTime;
        [SerializeField] private float extraJumps;
        [SerializeField] private float extraJumpsValue;
        [SerializeField] private float variableJumpHeight = 0.5f;

        public bool isJumping;
        public bool canJump;

        WallJumpCode wjc;
        WallChecker wc;

        FlipChar fc;

        DashAfterimage da;
        void Start()
        {
            wjc = GetComponent<WallJumpCode>();
            pw = GetComponent<PlayerWalk>();
            gc = GetComponent<GroundCheck>();
            fc = GetComponent<FlipChar>();
            extraJumps = extraJumpsValue;
            da = GetComponent<DashAfterimage>();
            wc = GetComponent<WallChecker>();
        }
        void Update()
        {


            isJumping = true;
            if (pw.rb.velocity.y <= 0.0f)
            {
                isJumping = false;
            }
            if ((gc.onGround && pw.rb.velocity.y <= 0.0f) || wc.isWallSliding)
            {
                extraJumps = extraJumpsValue;
            }
            if ((Input.GetKeyDown(KeyCode.Space) && extraJumps > 0 && !wc.isWallSliding))
            {
                pw.rb.velocity = new Vector2(pw.rb.velocity.x, jumpForce);
                extraJumps--;

            }
            else if (wc.isWallSliding && Input.GetKeyDown(KeyCode.Space) && !isJumping)
            {
                wc.isWallSliding = false;
                extraJumps--;
                Vector2 forceToAdd = new Vector2(wjc.wallHopForce * wjc.wallHopDirection.x * -wjc.facingDirection, wjc.wallHopForce * wjc.wallHopDirection.y);
                pw.rb.AddForce(forceToAdd, ForceMode2D.Impulse);

            }
            //    else if ((wc.isWallSliding ) && Input.GetAxis("Horizontal") > 0 && !isJumping)
            //  {
            //    wc.isWallSliding = false;
            //  extraJumps--;
            // Vector2 forceToAdd = new Vector2(wjc.wallJumpForce * wjc.wallJumpDirection.x * Input.GetAxis("Horizontal"), wjc.wallJumpForce * wjc.wallJumpDirection.y);
            //pw.rb.AddForce(forceToAdd, ForceMode2D.Impulse);
            //  }


            if (Input.GetButtonUp("Jump"))
            {
                pw.rb.velocity = new Vector2(pw.rb.velocity.x, pw.rb.velocity.y * variableJumpHeight);
            }

        }




        private void Jump()
        {
            if (!wc.isWallSliding && canJump)
            {
                pw.rb.velocity = new Vector2(pw.rb.velocity.x, jumpForce); ;
                extraJumps--;
            }
            else if (wc.isWallSliding && Input.GetAxis("Horizontal") == 0 && canJump) //Wall hop
            {
                wc.isWallSliding = false;
                extraJumps--;
                Vector2 forceToAdd = new Vector2(wjc.wallHopForce * wjc.wallHopDirection.x * -wjc.facingDirection, wjc.wallHopForce * wjc.wallHopDirection.y);
                pw.rb.AddForce(forceToAdd, ForceMode2D.Impulse);
            }
            else if ((wc.isWallSliding || wc.isTouchingWall) && Input.GetAxis("Horizontal") != 0 && canJump)
            {
                wc.isWallSliding = false;
                extraJumps--;
                Vector2 forceToAdd = new Vector2(wjc.wallJumpForce * wjc.wallJumpDirection.x * Input.GetAxis("Horizontal"), wjc.wallJumpForce * wjc.wallJumpDirection.y);
                pw.rb.AddForce(forceToAdd, ForceMode2D.Impulse);
            }
        }


    }
}

我会使用 2d 碰撞器来检查您是否接触到地面物体。然后在你的移动方法上我会检查你是否在触摸它。如果不是,请更改移动速度。