protected override void OnInitialize() { // Calculate max on all the speeds to make sure it's correct when spawning the player. walkingSpeeds.CalculateMax(); runSpeeds.CalculateMax(); crouchSpeeds.CalculateMax(); // Initialize the stamina module. stamina.Initialize(PlayerController, PlayerInput); // Initialize the moving platforms module. movingPlatforms.Initialize(PlayerController, PlayerInput); // Make the gravity + if needed. if (gravity < 0) { gravity = -gravity; } // Make the ground stick + if needed. if (groundStick < 0) { groundStick = -groundStick; } // Set the move to the walking speeds. moveSpeed = walkingSpeeds; // Calculate the real jump height. realJumpHeight = CalculateJumpHeight(jumpHeight); // Set the original controller height. originalControllerHeight = CharacterController.height; // Set the original controller center. originalControllerCenter = CharacterController.center; // Set the original camera position. originalCameraPosition = PlayerController.Camera.CameraHead.localPosition.y; // Calculate the crouch center for the character controller. controllerCrouchCenter = CrouchHeight / 2; // Calculate the camera position for when crouching. crouchCameraPosition = PlayerController.Camera.CameraHead.localPosition.y - (CharacterController.height - crouchHeight); // Set the current crouch camera position to the original camera position. currentCrouchCameraPosition = originalCameraPosition; }
/// <summary> /// Handles crouching. /// </summary> protected virtual void Crouching(float deltaTime) { // Only run the code if we can crouch. If we can't, always set 'isCrouching' to false. if (canCrouch) { switch (crouchToggleMode) { case CrouchToggleMode.Off: { shouldCrouch = GetButton(input_Crouch); break; } case CrouchToggleMode.Permanent: { bool crouchButtonPressed = GetButtonDown(input_Crouch); if (crouchButtonPressed) { shouldCrouch = !shouldCrouch; } break; } } // If the player wants to be crouching, set is crouching to true. // Else if we can stand up and we are crouching, stop crouching. if (shouldCrouch) { isCrouching = true; } else if (canStandUp && isCrouching && !shouldCrouch) { // If the player was previously crouched, fire the OnEndCrouch event, as the player is longer crouching. if (previouslyCrouched) { #if NET_4_6 || (UNITY_2018_3_OR_NEWER && !NET_LEGACY) OnEndCrouch?.Invoke(); #else if (OnEndCrouch != null) { OnEndCrouch.Invoke(); } #endif } // Set 'isCrouching' to false. isCrouching = false; // Set the character controller height to the original height we got at the start. CharacterController.height = originalControllerHeight; // Set the character controller center to the original center we got at the start. CharacterController.center = originalControllerCenter; // Set the move speed to the walking speed. moveSpeed = walkingSpeeds; // The player was not previously crouched. previouslyCrouched = false; } // Only do the code if the player is crouching. if (isCrouching) { // If the player wasn't previously crouched, fire the OnBeginCrouch event, as the player is now crouching. if (!previouslyCrouched) { #if NET_4_6 || (UNITY_2018_3_OR_NEWER && !NET_LEGACY) OnBeginCrouch?.Invoke(); #else if (OnBeginCrouch != null) { OnBeginCrouch.Invoke(); } #endif } // Check if we can stand up and update the 'canStandUp' value. canStandUp = CheckCanStandUp(); // Set the character controller height to the crouch height. CharacterController.height = crouchHeight; // Set the character controller center to the crouch center. CharacterController.center = new Vector3(CharacterController.center.x, controllerCrouchCenter, CharacterController.center.z); // Set the move speed to the crouch speed. moveSpeed = crouchSpeeds; // The player was previously crouched. previouslyCrouched = true; } // Lerp the current crouch camera position to either the crouch camera position or the original camera position. currentCrouchCameraPosition = Mathf.Lerp(currentCrouchCameraPosition, isCrouching ? crouchCameraPosition : originalCameraPosition, crouchHeadLerp * deltaTime); // Set the camera head position to the current crouch camera position. PlayerController.Camera.CameraHead.localPosition = new Vector3(PlayerController.Camera.CameraHead.localPosition.x, currentCrouchCameraPosition, PlayerController.Camera.CameraHead.localPosition.z); } else { // We can't crouch, always set 'isCrouching' to false. isCrouching = false; } }
/// <summary> /// Handles running. /// </summary> protected virtual void Running() { // If the player can't run, just stop here. if (!canRun) { return; } // Set 'isRunning' to true if the player velocity is above the walking speed max. isRunning = new Vector2(CharacterController.velocity.x, CharacterController.velocity.z).magnitude > walkingSpeeds.Max + 0.5f; bool runButtonPressed = GetButtonDown(input_Run); bool runButtonDown = GetButton(input_Run); switch (runToggleMode) { case RunToggleMode.Off: { shouldRun = runButtonDown; break; } case RunToggleMode.Permanent: { if (runButtonPressed) { shouldRun = !shouldRun; } break; } case RunToggleMode.UntilNoInput: { if (!hasUserInput) { shouldRun = false; } else if (!isRunning && !didRunSinceLastBreakInMovement && runButtonDown) { shouldRun = true; } else if (runButtonPressed) { shouldRun = !shouldRun; } break; } } // Only run if we're not crouching, can run, and the player wants to be running. if (!isCrouching && canRun && shouldRun) { // If stamina is enabled, only set move speed when stamina is above 0. // Else if stamina is not enabled, simply set move speed to run speeds. // Else if stamina is enabled and current stamina is 0 (or less), set move speed to walking speed. if (stamina.EnableStamina && stamina.CurrentStamina > 0) { moveSpeed = runSpeeds; } else if (!stamina.EnableStamina) { moveSpeed = runSpeeds; } else if (stamina.CurrentStamina <= 0) { moveSpeed = walkingSpeeds; } } else if (!isCrouching && !shouldRun) { // If we're not crouching and not holding down the run button, walk. moveSpeed = walkingSpeeds; } // Only run if m_isRunning is true. if (isRunning) { didRunSinceLastBreakInMovement = true; // If the player wasn't previously running, they just started. Fire the OnBeginRun event. if (!previouslyRunning) { #if NET_4_6 || (UNITY_2018_3_OR_NEWER && !NET_LEGACY) OnBeginRun?.Invoke(); #else if (OnBeginRun != null) { OnBeginRun.Invoke(); } #endif } // The player was previously running. previouslyRunning = true; } else { // If the player was previously running, fire the OnEndRun event. if (previouslyRunning) { #if NET_4_6 || (UNITY_2018_3_OR_NEWER && !NET_LEGACY) OnEndRun?.Invoke(); #else if (OnEndRun != null) { OnEndRun.Invoke(); } #endif } // The player is no longer running. previouslyRunning = false; } }