示例#1
0
        // This function is called every fixed framerate frame.
        void FixedUpdate()
        {
            // If the player should jump...
            player.rigidbody.gravityScale = 2;
            if (jump)
            {
                // If the player is jumping down a platform and is doing the first jump...
                if (player.jumpDown && jumps == doubleJumping.totalJumps)
                {
                    // ... add a small Y force.
                    player.rigidbody.AddForce(new Vector2(0f, 200f));

                    // If the player is on a platform...
                    if (player.OnPlatform())
                    {
                        // Set the moving platform to null.
                        player.UnstickFromPlatform();
                    }

                    // Reset the jumping variables.
                    ResetJumpVars();

                    // Reset jumpDown.
                    player.jumpDown = false;

                    // Stop the player from jumping.
                    return;
                }

                // If this is the initial jump...
                if (initialJump)
                {
                    // Check if the player is running when the jump is being performed.
                    walkingOnJump = player.walking;

                    // When double jumping, set Y velocity to 0 to make sure the jump force is applied correctly.
                    if (doubleJump)
                    {
                        player.SetYVelocity(0);
                    }

                    // Decrease total jumps allowed with 1.
                    jumps--;

                    // If the player is on a moving platform and should not keep the speed when jumping from a moving platform...
                    if (player.OnMovingPlatform() && !player.KeepSpeedOnJump())
                    {
                        // Set the X velocity to 0.
                        player.SetXVelocity(0);
                    }
                }

                // Get the jump factor.
                float jumpFactor = player.GetJumpFactor();

                // If you need to hold the Jump input to jump higher...
                if (jumpType == JumpType.HoldToJumpHigher)
                {
                    // When there is an initial jump...
                    if (initialJump)
                    {
                        // ... set the y velocity to the player's initial jump value.
                        float yVel = jumpFactor * (doubleJump ? holdToJumpHigher.initialDoubleJump : holdToJumpHigher.initialJump);

                        // If the player is on a moving platform...
                        if (player.OnMovingPlatform())
                        {
                            // ... get the current platform.
                            GameObject  platform          = player.GetPlatform();
                            Rigidbody2D platformRigidbody = platform.GetComponent <Rigidbody2D>();
                            // If the platform's Y velocity is greater than 0...
                            if (platformRigidbody.velocity.y > 0)
                            {
                                // ... make sure the y velocity of this platform is taken into account when jumping.
                                yVel += platformRigidbody.velocity.y;
                            }
                        }

                        // If the player is on a platform...
                        if (player.OnPlatform())
                        {
                            // Set the moving platform to null.
                            player.UnstickFromPlatform();
                        }

                        // Make sure the player's velocity is set.
                        player.SetYVelocity(yVel);

                        // Set initialJump to false.
                        initialJump = false;
                        // When the jump button is being pressed and the timer isn't finished yet...
                    }
                    else if (Input.GetButton("Jump") && jumpTimer > 0)
                    {
                        // ... decrease the timer's value.
                        jumpTimer -= Time.deltaTime;
                        // Set the Y Force for the player.
                        player.rigidbody.AddForce(new Vector2(0f, jumpFactor * (doubleJump ? holdToJumpHigher.doubleJumpForce : holdToJumpHigher.jumpForce)));
                    }
                    // Holding the jump button creates drag
                    else if (Input.GetButton("Jump") && airDrag)
                    {
                        player.rigidbody.gravityScale       = airDragGravityScale;
                        timeBeforeRestoreXPosition          = 0;
                        player.rigidbody.transform.position = new Vector2(player.rigidbody.transform.position.x - Time.deltaTime, player.rigidbody.transform.position.y);

                        // When the timer is finished or the jump button isn't being pressed...
                    }
                    else
                    {
                        // ... reset the jumping variables.
                        ResetJumpVars();
                    }
                    // Or else if you need a single press to perform a jump...
                }
                else
                {
                    // Add a vertical force to the player.
                    player.rigidbody.AddForce(new Vector2(0f, jumpFactor * (doubleJump ? singlePressToJump.doubleJumpForce : singlePressToJump.jumpForce)));

                    // If the player is on a platform...
                    if (player.OnPlatform())
                    {
                        // Set the moving platform to null.
                        player.UnstickFromPlatform();
                    }

                    // Reset the jumping variables.
                    ResetJumpVars();
                }
            }
            else
            {
                timeBeforeRestoreXPosition += Time.deltaTime;
                //Restore position to default x position
                if (player.rigidbody.transform.position.x < player.defaultXPosition && timeBeforeRestoreXPosition > restoreDelay)
                {
                    player.rigidbody.transform.position = new Vector2(Mathf.Min(player.rigidbody.transform.position.x + 1 * Time.deltaTime, player.defaultXPosition), player.rigidbody.transform.position.y);
                }
            }
        }