void FixedUpdate()
        {
            // Clear movement
            if (cancelMovement)
            {
                moveDirection  = Vector3.zero;
                cancelMovement = false;
                groundMotor.ClearActivePlatform();
                acrobatMotor.ClearFallingDamage();
                return;
            }

            // Handle freeze movement
            if (freezeMotor > 0)
            {
                freezeMotor -= Time.deltaTime;
                if (freezeMotor <= 0)
                {
                    freezeMotor    = 0;
                    CancelMovement = true;
                }
                return;
            }

            // Handle climbing
            climbingMotor.ClimbingCheck(ref collisionFlags);

            if (climbingMotor.IsClimbing)
            {
                acrobatMotor.Falling = false;
            }

            // Do nothing if player levitating/swimming or climbing - replacement motor will take over movement for levitating/swimming
            if (levitateMotor && (levitateMotor.IsLevitating || levitateMotor.IsSwimming) || climbingMotor.IsClimbing)
            {
                return;
            }

            // Player assumed to be in movement for now
            standingStill = false;

            if (grounded)
            {
                // Set standing still while grounded flag
                // Casting moveDirection to a Vector2 so constant downward force of gravity not included in magnitude
                standingStill = (new Vector2(moveDirection.x, moveDirection.z).magnitude == 0);

                acrobatMotor.Jumping = false;

                acrobatMotor.CheckFallingDamage();

                // checks if sliding and applies movement to moveDirection if true
                frictionMotor.MoveIfSliding(ref moveDirection);

                acrobatMotor.HandleJumpInput(ref moveDirection);
            }
            else
            {
                acrobatMotor.CheckInitFall();

                acrobatMotor.CheckAirControl(ref moveDirection, speed);
            }

            acrobatMotor.ApplyGravity(ref moveDirection);

            // If we hit something above us AND we are moving up, reverse vertical movement
            if ((controller.collisionFlags & CollisionFlags.Above) != 0)
            {
                if (moveDirection.y > 0)
                {
                    moveDirection.y = -moveDirection.y;
                }
            }

            groundMotor.MoveOnGround(moveDirection, ref collisionFlags, ref grounded);
        }
        void FixedUpdate()
        {
            // Check if on a solid surface
            grounded = (collisionFlags & CollisionFlags.Below) != 0;

            // Clear movement
            if (cancelMovement)
            {
                moveDirection  = Vector3.zero;
                cancelMovement = false;
                groundMotor.ClearActivePlatform();
                acrobatMotor.ClearFallingDamage();
                return;
            }

            // Handle freeze movement
            if (freezeMotor > 0)
            {
                freezeMotor -= Time.deltaTime;
                if (freezeMotor <= 0)
                {
                    freezeMotor    = 0;
                    CancelMovement = true;
                }
                return;
            }

            playerScanner.FindHeadHit(new Ray(controller.transform.position, Vector3.up));
            playerScanner.SetHitSomethingInFront();
            // Check if should hang
            hangingMotor.HangingChecks();
            // Handle Rappeling
            rappelMotor.RappelChecks();
            // Handle climbing
            climbingMotor.ClimbingCheck();

            // Do nothing if player levitating/swimming or climbing - replacement motor will take over movement for levitating/swimming
            if (levitateMotor && (levitateMotor.IsLevitating || levitateMotor.IsSwimming) || climbingMotor.IsClimbing || hangingMotor.IsHanging)
            {
                moveDirection = Vector3.zero;
                return;
            }


            if (climbingMotor.WallEject)
            {   // True in terms of the player having their feet on solid surface.
                grounded = true;
            }

            if (grounded)
            {
                acrobatMotor.Jumping = false;

                acrobatMotor.CheckFallingDamage();

                // checks if sliding and applies movement to moveDirection if true
                frictionMotor.GroundedMovement(ref moveDirection);

                acrobatMotor.HandleJumpInput(ref moveDirection);
            }
            else
            {
                acrobatMotor.CheckInitFall(ref moveDirection);

                acrobatMotor.CheckAirControl(ref moveDirection, speed);
            }

            playerScanner.FindStep(moveDirection);

            acrobatMotor.ApplyGravity(ref moveDirection);

            acrobatMotor.HitHead(ref moveDirection);

            groundMotor.MoveWithMovingPlatform(moveDirection);
        }
Exemple #3
0
        void FixedUpdate()
        {
            // Clear movement
            if (cancelMovement)
            {
                moveDirection  = Vector3.zero;
                cancelMovement = false;
                ClearActivePlatform();
                ClearFallingDamage();
                return;
            }

            // Handle freeze movement
            if (freezeMotor > 0)
            {
                freezeMotor -= Time.deltaTime;
                if (freezeMotor <= 0)
                {
                    freezeMotor    = 0;
                    CancelMovement = true;
                }
                return;
            }

            // Handle climbing
            climbingMotor.ClimbingCheck(ref collisionFlags);

            if (climbingMotor.IsClimbing)
            {
                falling = false;
            }

            // Do nothing if player levitating/swimming or climbing - replacement motor will take over movement for levitating/swimming
            if (levitateMotor && (levitateMotor.IsLevitating || levitateMotor.IsSwimming) || climbingMotor.IsClimbing)
            {
                return;
            }

            float inputX = InputManager.Instance.Horizontal;
            float inputY = InputManager.Instance.Vertical;
            // If both horizontal and vertical are used simultaneously, limit speed (if allowed), so the total doesn't exceed normal move speed
            float inputModifyFactor = (inputX != 0.0f && inputY != 0.0f && limitDiagonalSpeed) ? .7071f : 1.0f;

            // Cancel all movement input if player is paralyzed
            // Player should still be able to fall or move with platforms
            if (GameManager.Instance.PlayerEntity.IsParalyzed)
            {
                inputX = 0;
                inputY = 0;
            }

            // Player assumed to be in movement for now
            standingStill = false;

            if (grounded)
            {
                // Set standing still while grounded flag
                // Casting moveDirection to a Vector2 so constant downward force of gravity not included in magnitude
                standingStill = (new Vector2(moveDirection.x, moveDirection.z).magnitude == 0);

                if (jumping)
                {
                    jumping = false;
                }

                // If we were falling, and we fell a vertical distance greater than the threshold, run a falling damage routine
                if (falling)
                {
                    falling = false;
                    float fallDistance = fallStartLevel - myTransform.position.y;
                    if (fallDistance > fallingDamageThreshold)
                    {
                        FallingDamageAlert(fallDistance);
                    }
                    else if (fallDistance > fallingDamageThreshold / 2f)
                    {
                        BadFallDetected(fallDistance);
                    }
                    //if (myTransform.position.y < fallStartLevel - fallingDamageThreshold)
                    //    FallingDamageAlert(fallDistance);
                }

                // Get walking/crouching/riding speed
                speed = speedChanger.GetBaseSpeed();

                if (!riding)
                {
                    if (!isCrouching && heightChanger.HeightAction != HeightChangeAction.DoStanding) // don't set to standing height while croucher is standing the player
                    {
                        controller.height = standingHeight;
                    }

                    try
                    {
                        // If running isn't on a toggle, then use the appropriate speed depending on whether the run button is down
                        if (!toggleRun && InputManager.Instance.HasAction(InputManager.Actions.Run))
                        {
                            speed = speedChanger.GetRunSpeed(speed);
                        }
                    }
                    catch
                    {
                        speed = speedChanger.GetRunSpeed(speed);
                    }
                }

                // Handle sneak key. Reduces movement speed to half, then subtracts 1 in classic speed units
                if (InputManager.Instance.HasAction(InputManager.Actions.Sneak))
                {
                    speed /= 2;
                    speed -= (1 / PlayerSpeedChanger.classicToUnitySpeedUnitRatio);
                }

                // checks if sliding and applies movement to moveDirection if true
                frictionMotor.MoveIfSliding(ref moveDirection);

                try
                {
                    // Jump! But only if the jump button has been released and player has been grounded for a given number of frames
                    if (!InputManager.Instance.HasAction(InputManager.Actions.Jump))
                    {
                        jumpTimer++;
                    }
                    //if (!Input.GetButton("Jump"))
                    //    jumpTimer++;
                    else if (jumpTimer >= antiBunnyHopFactor)
                    {
                        moveDirection.y = jumpSpeed;
                        jumpTimer       = 0;
                        jumping         = true;

                        // Modify crouching jump speed
                        if (isCrouching)
                        {
                            moveDirection.y *= crouchingJumpDelta;
                        }
                    }
                    else
                    {
                        jumping = false;
                    }
                }
                catch
                {
                }
            }
            else
            {
                // If we stepped over a cliff or something, set the height at which we started falling
                if (!falling)
                {
                    falling        = true;
                    fallStartLevel = myTransform.position.y;
                }

                // If air control is allowed, check movement but don't touch the y component
                if (airControl && frictionMotor.PlayerControl)
                {
                    moveDirection.x = inputX * speed * inputModifyFactor;
                    moveDirection.z = inputY * speed * inputModifyFactor;
                    moveDirection   = myTransform.TransformDirection(moveDirection);
                }
            }

            // Apply gravity
            moveDirection.y -= gravity * Time.deltaTime;

            // If we hit something above us AND we are moving up, reverse vertical movement
            if ((controller.collisionFlags & CollisionFlags.Above) != 0)
            {
                if (moveDirection.y > 0)
                {
                    moveDirection.y = -moveDirection.y;
                }
            }

            // Moving platform support
            if (activePlatform != null)
            {
                var newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
                var moveDistance           = (newGlobalPlatformPoint - activeGlobalPlatformPoint);
                if (moveDistance != Vector3.zero)
                {
                    controller.Move(moveDistance);
                }
                //lastPlatformVelocity = (newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime;

                // If you want to support moving platform rotation as well:
                var newGlobalPlatformRotation = activePlatform.rotation * activeLocalPlatformRotation;
                var rotationDiff = newGlobalPlatformRotation * Quaternion.Inverse(activeGlobalPlatformRotation);

                // Prevent rotation of the local up vector
                rotationDiff = Quaternion.FromToRotation(rotationDiff * transform.up, transform.up) * rotationDiff;

                transform.rotation = rotationDiff * transform.rotation;
            }
            //else
            //{
            //    lastPlatformVelocity = Vector3.zero;
            //}

            activePlatform = null;

            // Move the controller, and set grounded true or false depending on whether we're standing on something
            collisionFlags = controller.Move(moveDirection * Time.deltaTime);

            grounded = (collisionFlags & CollisionFlags.Below) != 0;

            // Moving platforms support
            if (activePlatform != null)
            {
                activeGlobalPlatformPoint = transform.position;
                activeLocalPlatformPoint  = activePlatform.InverseTransformPoint(transform.position);

                // If you want to support moving platform rotation as well:
                activeGlobalPlatformRotation = transform.rotation;
                activeLocalPlatformRotation  = Quaternion.Inverse(activePlatform.rotation) * transform.rotation;
            }
        }