コード例 #1
0
ファイル: PlayerJump.cs プロジェクト: aaxu/Flying-Cat-Game
        // Update is called once per frame.
        void Update()
        {
            // Reset total jumps allowed when not performing a jump and grounded or when on a moving/sinking platform.
            if (!jump && (player.grounded || player.IsStuckToPlatform()) && ((Mathf.Round(player.rigidbody.velocity.y) == 0) || ((player.OnMovingPlatform() || player.OnSinkingPlatform()) && player.rigidbody.velocity.y == player.GetPlatform().GetComponent <Rigidbody2D>().velocity.y)))
            {
                jumps = doubleJumping.totalJumps;
            }

            // If the jump button is pressed, jumps are allowed and the player is not dashing, sliding, on a ladder or crouching under an obstacle...
            if (!jump && Input.GetButtonDown("Jump") && jumps > 0 && !player.dashing && !player.sliding && !player.onLadder && (!player.crouching || (player.crouching && player.AllowedToStandUp())))
            {
                // If the player is grounded...
                if (player.grounded)
                {
                    // ... initialize jump.
                    InitJump();
                    // If the player is not grounded and totalJumps is higher than 1...
                }
                else if (doubleJumping.totalJumps > 1)
                {
                    // ... initialize jump if the Y velocity is inside the double jump window (or when there isn't a window).
                    if (!doubleJumping.jumpWindow || (doubleJumping.jumpWindow && player.rigidbody.velocity.y > doubleJumping.jumpWindowMin && player.rigidbody.velocity.y < doubleJumping.jumpWindowMax))
                    {
                        doubleJump = true;
                        InitJump();
                    }
                }
            }
        }
コード例 #2
0
        // This function is called every fixed framerate frame.
        void FixedUpdate()
        {
            // If a dash should be performed...
            if (dash)
            {
                // ... reset the dash variable.
                dash = false;

                // Unstick from the current platform.
                player.UnstickFromPlatform();

                // Set dashing to true.
                player.Dash(true);

                // Reset the X velocity.
                player.SetXVelocity(0);

                // Add an X force to the rigid body to actually perform the dash.
                player.rigidbody.AddForce(new Vector2((player.facingRight ? 1 : -1) * dashForce, 0f));

                // Reset the dash timers.
                dashTimer     = dashTime;
                cooldownTimer = cooldownTime;

                // If there is an air dash limit, make sure it gets decreased.
                if (airDashLimit && airDashTotal > 0)
                {
                    totalAirDashes--;
                }
            }

            // If the player is currently dashing...
            if (player.dashing)
            {
                // ... make sure the Y velocity is set if the gravity is disabled for the dash.
                if (!dashGravity)
                {
                    // Set the speed.
                    float speed = dashVelocityY;

                    // If the player is on a moving platform, make sure the extra Y velocity is added.
                    if (player.OnMovingPlatform())
                    {
                        GameObject platform = player.GetPlatform();
                        float      yVel     = platform.GetComponent <Rigidbody2D>().velocity.y;
                        speed = speed + yVel;
                    }

                    // Set the Y velocity.
                    player.SetYVelocity(speed);
                }
            }
        }