コード例 #1
0
        // This function is called every fixed framerate frame.
        void FixedUpdate()
        {
            // Cache the run input.
            bool running = Input.GetButton("Run");

            // Get the current speed.
            float speed = GetSpeed();

            // Only make the player walk when pressToRun is active and the player isn't running.
            player.Walk(pressToRun && !running);

            // If the player is not stuck to the wall and the player is not dashing, sliding, crouching and/or on a ladder...
            if (!player.stuckToWall && !player.dashing && !player.sliding && !player.crouching && !player.onLadder)
            {
                // ... if the player is changing direction (h has a different sign to velocity.x) or hasn't reached speed yet...
                if (player.hor * player.rigidbody.velocity.x < speed)
                {
                    // ... add a force to the player.
                    player.rigidbody.AddForce(transform.rotation * Vector2.right * player.hor * player.GetMovementForce(moveForce));
                }
                // If the player's horizontal velocity is greater than the speed and the player isn't stuck to the X of a platform...
                if (Mathf.Abs(player.rigidbody.velocity.x) > speed && !player.IsStuckToPlatformX())
                {
                    // ... set the player's velocity to the speed in the x axis.
                    player.SetXVelocity(Mathf.Sign(player.rigidbody.velocity.x) * speed);
                }
            }
        }
コード例 #2
0
ファイル: PlayerCrouch.cs プロジェクト: aaxu/Flying-Cat-Game
        // 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;
            }
        }
コード例 #3
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);
                }
            }
        }
コード例 #4
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);
                }
            }
        }
コード例 #5
0
        // This function is called every fixed framerate frame.
        void FixedUpdate()
        {
            // Cache the vertical and horizontal input.
            float v = Input.GetAxis("Vertical");
            float h = Input.GetAxis("Horizontal");

            // If the player is on a ladder and the Ladder class is set...
            if (player.onLadder && ladder)
            {
                // Create the variables used to check if the player is almost falling off the ladder.
                bool    stillOnLadder = true;
                Vector2 pos           = transform.position;
                Vector2 pointA        = new Vector2(pos.x + boxCollider.offset.x - (boxCollider.size.x / 2), pos.y + boxCollider.offset.y - (boxCollider.size.y / 2));
                Vector2 pointB        = new Vector2(pos.x + boxCollider.offset.x + (boxCollider.size.x / 2), pos.y + boxCollider.offset.y + (boxCollider.size.y / 2));

                // If the player is moving down and movement down is allowed...
                if (v < 0 && ladder.allowDown)
                {
                    // ... check if the player is almost falling off the ladder if the player isn't allowed to fall off.
                    if (!ladder.fallOffBottom)
                    {
                        stillOnLadder = Physics2D.OverlapArea(pointA, new Vector2(pointB.x, pointB.y - 0.1f), ladderLayer);
                    }

                    // If the player isn't allowed to fall off and is almost falling off the ladder...
                    if (!ladder.fallOffBottom && !stillOnLadder)
                    {
                        // ... make sure there is no Y movement.
                        player.SetYVelocity(0);
                        // Or else...
                    }
                    else
                    {
                        // ... set the player's Y velocity based on the climbing and default speed.
                        player.SetYVelocity(v * (climingSpeedDown + (ladder.defaultSpeedY <= 0 ? ladder.defaultSpeedY : -ladder.defaultSpeedY)));
                    }
                    // Or else if the player is moving up and movement down is allowed...
                }
                else if (v > 0 && ladder.allowUp)
                {
                    // ... check if the player is almost falling off the ladder if the player isn't allowed to fall off.
                    if (!ladder.fallOffTop)
                    {
                        stillOnLadder = Physics2D.OverlapArea(new Vector2(pointA.x, pointA.y + 0.1f), pointB, ladderLayer);
                    }

                    // If the player isn't allowed to fall off and is almost falling off the ladder...
                    if (!ladder.fallOffTop && !stillOnLadder)
                    {
                        // ... make sure there is no Y movement.
                        player.SetYVelocity(0);
                        // Or else...
                    }
                    else
                    {
                        // ... set the player's Y velocity based on the climbing and default speed.
                        player.SetYVelocity(v * (climingSpeedUp + ladder.defaultSpeedY));
                    }
                    // Or else ...
                }
                else
                {
                    // ... set the player's speed to the ladder's default speed.
                    player.SetYVelocity(ladder.defaultSpeedY);
                }

                // If the player is grounded and is moving left or right...
                if (player.grounded && (h < 0 || h > 0))
                {
                    // ... unstick the player from the ladder.
                    Unstick();
                    // Or else if the player is snapped to the middle...
                }
                else if (ladder.snapToMiddle)
                {
                    // ... if the player is snapped to the middle and allowed to fall off the ladder and moving left or right...
                    if (snappedToMiddle && (v == 0) && ((ladder.fallOffLeft && h < 0) || (ladder.fallOffRight && h > 0)))
                    {
                        // ... unstick the player from the ladder.
                        Unstick();
                        // Or else...
                    }
                    else
                    {
                        // ... if the player is currently in the middle of the ladder...
                        if (transform.position.x > (hitLadder.transform.position.x - 0.05f) && transform.position.x < (hitLadder.transform.position.x + 0.05f))
                        {
                            // Make sure the X velocity is set to 0.
                            player.SetXVelocity(0);
                            // Make sure the player is snapped to the middle.
                            snappedToMiddle = true;
                            // Or else...
                        }
                        else
                        {
                            // ... calculate the velocity needed to move to the center of the ladder.
                            Vector2 gotoCenter = (new Vector3(hitLadder.transform.position.x, transform.position.y, 0) - transform.position).normalized * snapSpeed;
                            // Set the X velocity to this velocity.
                            player.SetXVelocity(gotoCenter.x);
                            // Make sure the player isnt't snapped to the middle.
                            snappedToMiddle = false;
                        }
                    }
                    // Or else...
                }
                else
                {
                    // ... if the player is moving to the left and is allowed to move to the left...
                    if (h < 0 && ladder.allowLeft)
                    {
                        // ... check if the player is almost falling off the ladder if the player isn't allowed to fall off.
                        if (!ladder.fallOffLeft)
                        {
                            //
                            stillOnLadder = Physics2D.OverlapArea(pointA, new Vector2(pointB.x - 0.1f, pointB.y), ladderLayer);
                        }

                        // If the player isn't allowed to fall off and is almost falling off the ladder...
                        if (!ladder.fallOffLeft && !stillOnLadder)
                        {
                            // ... make sure the X velocity is set to 0.
                            player.SetXVelocity(0);
                            // Or else...
                        }
                        else
                        {
                            // ... set the player's X velocity based on the climbing and default speed.
                            player.SetXVelocity(h * (climingSpeedLeft + (ladder.defaultSpeedX <= 0 ? ladder.defaultSpeedX : -ladder.defaultSpeedX)));
                        }
                        // Or else if the player is moving to the right and is allowed to move to the right...
                    }
                    else if (h > 0 && ladder.allowRight)
                    {
                        // ... check if the player is almost falling off the ladder if the player isn't allowed to fall off.
                        if (!ladder.fallOffRight)
                        {
                            stillOnLadder = Physics2D.OverlapArea(new Vector2(pointA.x + 0.1f, pointA.y), pointB, ladderLayer);
                        }

                        // If the player isn't allowed to fall off and is almost falling off the ladder...
                        if (!ladder.fallOffRight && !stillOnLadder)
                        {
                            // ... make sure the X velocity is set to 0.
                            player.SetXVelocity(0);
                            // Or else...
                        }
                        else
                        {
                            // ... set the player's X velocity based on the climbing and default speed.
                            player.SetXVelocity(h * (climingSpeedRight + ladder.defaultSpeedX));
                        }
                        // Or else...
                    }
                    else
                    {
                        // ... set the player's X velocity to the ladder's X default speed.
                        player.SetXVelocity(ladder.defaultSpeedX);
                    }
                }
            }
        }
コード例 #6
0
ファイル: PlayerJump.cs プロジェクト: aaxu/Flying-Cat-Game
        // 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);
                }
            }
        }
コード例 #7
0
        // 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);
                }
            }
        }