Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一代码不允许字符(2D,刚体)在空中转向。如何使用此脚本获得更多的空中控制?_C#_Unity3d_2d_Game Physics - Fatal编程技术网

C# 统一代码不允许字符(2D,刚体)在空中转向。如何使用此脚本获得更多的空中控制?

C# 统一代码不允许字符(2D,刚体)在空中转向。如何使用此脚本获得更多的空中控制?,c#,unity3d,2d,game-physics,C#,Unity3d,2d,Game Physics,在为我的Unity游戏编写2D乐章时,我陷入了一个问题。之前我决定让角色不能在空中移动,但是,我想改变一下。我怎么做?我希望角色能够在空中旋转方向,但速度不同于“移动速度”。这是我第一次上这个网站,所以如果我透露了太多的细节,我很抱歉。以下是移动和跳跃脚本: 行走脚本 public bool canMove = true; [SerializeField] public Vector2 newVelocity; [SerializeField] pu

在为我的Unity游戏编写2D乐章时,我陷入了一个问题。之前我决定让角色不能在空中移动,但是,我想改变一下。我怎么做?我希望角色能够在空中旋转方向,但速度不同于“移动速度”。这是我第一次上这个网站,所以如果我透露了太多的细节,我很抱歉。以下是移动和跳跃脚本:

行走脚本

      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);
            }
        }


    }
}
public bool canMove=true;
[SerializeField]公共向量2 newVelocity;
[Serialized Field]公共刚体2d rb;
[SerializeField]公共浮点移动速度=10f;
[SerializeField]公共向量2方向;
[SerializeField]公共浮动墙SlideSpeed;
公众浮标移动部队;
公共浮子气吸式倍增管;
FlipChar-flipc;
地面检查gc;
斜坡路西南;
PlayerJump-pj;
WallChecker wc;
达什达;
无效唤醒()
{
gc=GetComponent();
sw=GetComponent();
rb=GetComponent();
pj=GetComponent();
wc=GetComponent();
da=GetComponent();
}
void Start()
{
flipc=GetComponent();
}
无效更新()
{
}
void FixedUpdate()
{
如果(gc.onGround&&canMove&&da.dash)
{
rb.velocity=新矢量2(Input.GetAxis(“水平”)*moveSpeed,rb.velocity.y);
}
如果(!gc.onGround&&!wc.isWallSliding&&Input.GetAxis(“水平”)!=0,则为else
{
Vector2 forceToAdd=新矢量2(movementForceAir*Input.GetAxis(“水平”),0);
rb.添加力(forceToAdd);
如果(平均绝对速度(rb.velocity.x)>移动速度)
{
rb.velocity=newvector2(moveSpeed*Input.GetAxis(“水平”),rb.velocity.y);
//}否则如果(!gc.onGround&&!wc.isWallSliding&&Input.GetAxis(“水平”)==0){
//rb.velocity=新矢量2(rb.velocity.x,rb.velocity.y);
}
}
如果(厕所壁滑动)
{
if(rb.velocity.y
跳转脚本

      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);
            }
        }


    }
}
[hideininstect]公共地面检查gc;
[Hideininstect]公共PlayerWalk pw;
[Field]私人浮动跳线部队;
[SerializeField]专用浮点跳时计数器;
[SerializeField]专用浮点跳时;
[SerializeField]专用浮点跳转;
[SerializeField]私有浮点值;
[SerializeField]专用浮点变量JumpHeight=0.5f;
公共场所在跳跃;
公共场所可以跳跃;
代码wjc;
WallChecker wc;
FlipChar-fc;
达什达;
void Start()
{
wjc=GetComponent();
pw=GetComponent();
gc=GetComponent();
fc=GetComponent();
extraJumps=extraJumpsValue;
da=GetComponent();
wc=GetComponent();
}
无效更新()
{
isJumping=true;
if(pw.rb.velocity.y0&&!isJumping)
//  {
//wc.isWallSliding=false;
//额外跳跃--;
//Vector2 forceToAdd=新矢量2(wjc.wallJumpForce*wjc.wallJumpDirection.x*Input.GetAxis(“水平”),wjc.wallJumpForce*wjc.wallJumpDirection.y);
//pw.rb.AddForce(forceToAdd,ForceMode2D.pulse);
//  }
if(Input.GetButtonUp(“跳转”))
{
pw.rb.velocity=新矢量2(pw.rb.velocity.x,pw.rb.velocity.y*可变跳跃高度);
}
}
私有无效跳转()
{
如果(!wc.isWallSliding&&canJump)
{
pw.rb.velocity=新矢量2(pw.rb.velocity.x,跳跃力);
额外跳跃--;
}
else if(wc.isWallSliding&&Input.GetAxis(“水平”)==0&&canJump)//墙跃点
{
wc.isWallSliding=false;
额外跳跃--;
Vector2 forceToAdd=新矢量2(wjc.wallHopForce*wjc.wallHopForce.x*-wjc.facingDirection,wjc.wallHopForce*wjc.wallHopForce.y);
pw.rb.AddForce(forceToAdd,ForceMode2D.pulse);
}
else如果((wc.iswall滑动| | wc.isTouchingWall)和&Input.GetAxis(“水平”)!=0和&canJump)
{
wc.isWallSliding=false;
额外跳跃--;
Vector2 forceToAdd=新矢量2(wjc.wallJumpForce*wjc.wallJumpDirection.x*Input.GetAxis(“水平”),wjc.wallJumpForce*wjc.wallJumpDirection.y);
pw.rb.AddForce(forceToAdd,ForceMode2D.pulse);
}
}
}
}

我将使用2d碰撞器检查您是否接触地面对象。然后在你的移动方式上,我会检查你是否触摸它。如果您没有,请更改移动速度。

另外,我会在开始时为“isJumping”bool添加您的If语句,以确保您的游戏不会出现问题谢谢您的帮助!你是说在一开始就把isJumping bool设为false?我是说先把这个设为if(pw.rb.velocity.y)