예제 #1
0
        // 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;
            }
        }
예제 #2
0
        // This function is called every fixed framerate frame.
        void FixedUpdate()
        {
            // Make the player sticky to the moving platform.
            if (OnMovingPlatform())
            {
                // Cache the platform's velocity.
                Vector2 platformVelocity = platformClass.rigidbody.velocity;

                // If the player should stick to the X velocity of the platform...
                if (movingPlatform.stickToX && !unstick)
                {
                    // ... cache the player's X velocity.
                    float xVel = player.rigidbody.velocity.x;
                    // Get the player's speed.
                    float speed = player.GetSpeed(false);
                    // Set the min and max velocity.
                    float minVel = platformVelocity.x;
                    float maxVel = platformVelocity.x;

                    // If the player should stop...
                    if (player.hor == 0)
                    {
                        // If the player should use the friction of the platform...
                        if (movingPlatform.useFriction)
                        {
                            // Simulate friction on the platform.
                            // Doesn't work as good as non-moving platforms.
                            // If you have a better solution, let us know: [email protected].

                            // If stoppedMoving isn't set to true yet...
                            if (!stoppedMoving)
                            {
                                // ... set stoppedMoving to true.
                                stoppedMoving = true;
                                // Set the total slowdown to the player's current X velocity.
                                totalSlowdown = Mathf.Abs(platformVelocity.x - lastXVel);
                            }

                            // If the total slowdown is below 0 (and the player should stop moving)...
                            if (totalSlowdown <= 0)
                            {
                                // ... set the previousHor variable to 0.
                                previousHor = 0;
                            }

                            // If the previousHor variable isn't 0 (so the player stopped moving, but hasn't fully stopped yet due to friction)...
                            if (previousHor != 0)
                            {
                                // ... decrease the total slowdown by the slowdown set by the frictions of the player and platform.
                                totalSlowdown -= slowdown;
                                // If the player was moving to the right...
                                if (previousHor == 1)
                                {
                                    // ... make sure the player's velocity is the same as the platform's plus the total slowdown.
                                    player.SetXVelocity(platformVelocity.x + totalSlowdown);
                                    // Or else if the player was moving to the left...
                                }
                                else if (previousHor == -1)
                                {
                                    // ... make sure the player's velocity is the same as the platform's minus the total slowdown.
                                    player.SetXVelocity(platformVelocity.x - totalSlowdown);
                                }
                                // Or when the previousHor variable is 0 (so the player actually stopped moving, because totalSlowdown is below 0)...
                            }
                            else
                            {
                                // ... set the player's X velocity to that of the platform, so the player doesn't fall off.
                                player.SetXVelocity(platformVelocity.x);
                            }
                            // Or else...
                        }
                        else
                        {
                            // Directly set the player's velocity to the platform's velocity.
                            player.SetXVelocity(platformVelocity.x);
                        }
                        // Or else if the player is moving...
                    }
                    else
                    {
                        // ... make sure the player sticks to the platform during movement.

                        // If the player should use the platform's friction...
                        if (movingPlatform.useFriction)
                        {
                            // ... make sure stoppedMoving is false and remember the previous horizontal input.
                            stoppedMoving = false;
                            previousHor   = player.hor;
                        }

                        // Set the minimum and maximum velocity based on the player's and platform's direction.
                        if ((platformVelocity.x < 0 && player.hor > 0) || (platformVelocity.x > 0 && player.hor > 0))
                        {
                            minVel = platformVelocity.x;
                            maxVel = platformVelocity.x + speed;
                        }
                        else if ((platformVelocity.x < 0 && player.hor < 0) || (platformVelocity.x > 0 && player.hor < 0))
                        {
                            minVel = platformVelocity.x - speed;
                            maxVel = platformVelocity.x;
                        }

                        // Remember the current X velocity on the platform.
                        lastXVel = platformVelocity.x + (player.hor * speed);
                        // Set the player's velocity to this velocity if the player's current velocity is lower than the minimum or higher than the maximum.
                        if (xVel > maxVel || xVel < minVel)
                        {
                            player.SetXVelocity(lastXVel);
                        }
                    }
                }

                // If the player should stick to the Y velocity of the platform...
                if (movingPlatform.stickToY && !unstick)
                {
                    // ... set the Y velocity based on the platform's Y velocity.
                    player.SetYVelocity(platformVelocity.y);
                }
            }
        }
        // This function is called every fixed framerate frame.
        void FixedUpdate()
        {
            // Cache the player speed and horizontal input.
            float speed = player.GetSpeed();

            // Make the player performs a boomerang wall jump.
            if (boomerangJump)
            {
                // Perform the jump once.
                if (performBoomerangJump)
                {
                    performBoomerangJump = false;
                    boomerangDirection   = player.hor;
                    player.SetYVelocity(0);
                    PerformWallJump();
                }

                // If the boomerang wall jumping direction is different, stop the boomerang wall jump.
                if (player.hor != boomerangDirection)
                {
                    boomerangJump = false;
                    // Or else set the speed in the opposite direction.
                }
                else
                {
                    player.SetXVelocity(speed * (player.hor * -1));
                }
            }
            else
            {
                // Set the velocity to make the player run on a wall.
                if (wallRunning)
                {
                    if (!wallRun.infiniteRun && wallRun.runSlowdown)
                    {
                        player.SetYVelocity(runSlowdownSpeed);
                    }
                    else
                    {
                        player.SetYVelocity(wallRun.runSpeed);
                    }
                }

                // Set the velocity to make the player slide down a wall.
                if (wallSliding)
                {
                    if (wallSlide.slideSpeedup)
                    {
                        player.SetYVelocity(-slideSpeedupSpeed);
                    }
                    else
                    {
                        player.SetYVelocity(-wallSlide.slideSpeed);
                    }
                }

                // Make the player stuck on the wall to wall jump.
                if (stuckToWall)
                {
                    player.SetXVelocity(0);
                    player.SetYVelocity(wallJump.wallStickVelocity);
                }
            }
        }