// 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); } } }
// This function is called every fixed framerate frame. void FixedUpdate() { // If crouching and movement is allowed... if (player.crouching && allowMovement && !player.sliding && player.hor != 0) { // ... set X velocity to the speed times the horizontal input (between -1 and 1). player.SetXVelocity(player.GetSpeed() * player.hor); } // If a slide should be performed... if (slide) { // ... reset the slide variable. slide = false; // Unstick from current platform. player.UnstickFromPlatform(); // If the player is in the air and an air slide is allowed... if (allowAirSlide && !player.grounded) { // ... reset the X and Y velocity. player.SetXVelocity(0); player.SetYVelocity(0); // Add an X and Y force to the rigid body to actually perform the slide. player.rigidbody.AddForce(new Vector2((player.facingRight ? 1 : -1) * airSlideForce.x, airSlideForce.y)); // Or else... } else { // ... reset the X velocity. player.SetXVelocity(0); // Add an X force to the rigid body to actually perform the slide. player.rigidbody.AddForce(new Vector2((player.facingRight ? 1 : -1) * slideForce, 0f)); } // Reset the slide timers. slideTimer = slideTime; cooldownTimer = cooldownTime; } }
// 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); } } }