简体   繁体   English

流畅的双跳Unity 2D平台游戏

[英]Smooth double jump Unity 2D platformer

I'm trying to make a smooth double jump mechanic for Unity 2D, platformer game but right now the second jump is bigger if you jump right after the initial jump and I want to have it the other way around the double jump should throw you higher if you press it later like in most 2D platformer games this is my code 我正在尝试为Unity 2D和平台游戏制作流畅的双跳机制,但现在如果您在初始跳后立即跳,第二跳会更大,而我想以另一种方式绕双跳应该将您扔得更高如果稍后像大多数2D平台游戏中一样按下它,这就是我的代码

       private void Update()
       {
            grounded = Physics2D.OverlapArea(transform.position, GroundedEnd.position, 1 << LayerMask.NameToLayer("Ground"));
            if (grounded || !doubleJumpUsed)
            {
                playerRigidBody.AddForce(new Vector2(0, JumpPower));
            }
            if (!grounded && !doubleJumpUsed)
            {
                doubleJumpUsed = true;
            }
            UpdateJumpingAnimator();
        }

        private void UpdateJumpingAnimator()
        {
           if (grounded)
           {
               doubleJumpUsed = false;
           }
        }

Try this code: 试试这个代码:

playerRigidBody.AddForce(new Vector2(0, JumpPower), ForceMode2D.Impulse);

In this case it is necessary to reduce the value of the variable JumpPower 在这种情况下,有必要减小变量JumpPower的值

Honestly I'm not sure if there's a pretty way to do what you're asking. 老实说,我不确定是否有一种不错的方法来完成您要问的事情。 But have you tried tieing JumpPower into a timer? 但是您是否尝试过将JumpPower连接到计时器中? In other words, the longer the character is in the air the more time has elapsed, the more force is applied to the 2'nd jump when the button is pressed. 换句话说,角色在空中的时间越长,经过的时间越长,按下按钮时对第二跳的作用力就越大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM