using static scr_Models; using UnityEngine; public class scr_PlayerController : MonoBehaviour { CharacterController characterController; // creates a reference to the character controller component of this player Animator characterAnimator; PlayerInputActions playerInputActions; // creates a variable of type PlayerInputActions called playerInputActions //[HideInInspector] // so these won't be seen public Vector2 input_Movement; [HideInInspector] // so these won't be seen public Vector2 input_View; //[HideInInspector] public float jumpingTimer; Vector3 playerMovement; public PlayerSettingsModel settings; public bool isAimingMode; [Header("Movement")] public float movementSpeedOffset = 1; public bool isWalking; public bool isSprinting; public float movementSmoothDamp; [Header("Camera")] public Transform cameraTarget; public scr_CameraController cameraController; [HideInInspector] public float verticalSpeed; private float targetVerticalSpeed; private float verticalSpeedVelocity; [HideInInspector] public float horizontalSpeed; private float targetHorizontalSpeed; private float horizontalSpeedVelocity; [Header("Stats")] public PlayerStatsModel stats; private void Awake() { Cursor.lockState = CursorLockMode.Locked; // locks the cursor to the middle of the screen when you click the screen characterController = GetComponent(); // gives the characterController variable a value of this player's character controller component characterAnimator = GetComponent(); // gives characterAnimator variable a value of this player's Animator component #region - Input related script - playerInputActions = new PlayerInputActions(); // creates a new instance of a PlayerInputActions for the private variable playerInputActions to be equal to #region - Input events - playerInputActions.Movement.Movement.performed += e => input_Movement = e.ReadValue(); // sets the value of input_Movement playerInputActions.Movement.View.performed += e => input_View = e.ReadValue(); // sets the value of input_View playerInputActions.Actions.Jump.performed += e => Jump(); playerInputActions.Actions.SuperJump.performed += e => SuperJump(); playerInputActions.Actions.WalkingToggle.performed += e => ToggleWalking(); playerInputActions.Actions.Sprint.performed += e => Sprint(); #endregion #endregion } private void FixedUpdate() { //Movement(); } private void Update() { JumpingTimer(); Movement(); CalculateSprint(); } private void CalculateSprint() { if (!CanSprint()) // if CanSprint returns false { isSprinting = false; // set isSprinting to false } // if CanSprint returns false, set isSprinting to false if (isSprinting) // if isSprinting is true { if (stats.Stamina > 0) // if the player's stamina is greater than 0 { stats.Stamina -= stats.StaminaDrain * Time.deltaTime; // drain Stamina by StaminaDrain every second } // if the player's stamina is greater than 0, drain the Stamina else // if you run out of Stamina { isSprinting = false; } // if you run out of Stamina, set isSprinting to false stats.StaminaCurrentDelay = stats.StaminaDelay; // if isSprinting is true, set StaminaCurrentDelay = StaminaDelay } // if isSprinting is true else // if isSprinting is false { if (stats.StaminaCurrentDelay <= 0) // if StaminaCurrentDelay is less than or equal to 0 { if (stats.Stamina < stats.MaxStamina) // if your Stamina is less than your MaxStamina { stats.Stamina += stats.StaminaRegain * Time.deltaTime; // restores your Stamina by StaminaRegain every second } // if your Stamina is less than your MaxStamina, start regaining your Stamina else // if your Stamina is not less than your MaxStamina { stats.Stamina = stats.MaxStamina; // sets Stamina equal to MaxStamina so Stamina never exceeds MaxStamina } // if your Stamina is not less than your MaxStamina, set Stamina = MaxStamina so it Stamina never exceeds MaxStamina } else // if StaminaCurrentDelay > 0 { stats.StaminaCurrentDelay -= Time.deltaTime; // drain StaminaCurrentDelay every second } // if StaminaCurrentDelay > 0 } // if isSprinting is false } private void Movement() { if (isAimingMode) // if isAimingMode is true { if (input_Movement.y > 0) // if you are pushing forward { targetVerticalSpeed = (isWalking ? settings.WalkingSpeed : settings.RunningSpeed); } // if you are pushing forward, check to see if isWalking is true or false and set the targetVerticalSpeed to reflect the Walking or Running speed in the settings. else { targetVerticalSpeed = (isWalking ? settings.WalkingBackwardSpeed : settings.RunningBackwardSpeed); } // else you would be pushing backwards targetHorizontalSpeed = (isWalking ? settings.WalkingStrafingSpeed : settings.RunningStrafingSpeed); } // if isAimingMode is true else // if isAimingMode is false { var originalRotation = transform.rotation; transform.LookAt(playerMovement + transform.position, Vector3.up); // makes the character look at where the camera is facing when you run var newRotation = transform.rotation; transform.rotation = Quaternion.Lerp(originalRotation, newRotation, settings.CharacterRotationSmoothDamp); // makes character rotation smooth float playerSpeed; // this float reflects the current player speed (which is usually set to WalkingSpeed, RunningSpeed, or SprintingSpeed in the settings) if (isSprinting) // if isSprinting is true { playerSpeed = settings.SprintingSpeed; // set playerSpeed to SprintingSpeed } else // if isSprinting is false { playerSpeed = (isWalking ? settings.WalkingSpeed : settings.RunningSpeed); // set playerSpeed to WalkingSpeed or RunningSpeed based on if isWalking is true or false } targetVerticalSpeed = playerSpeed; targetHorizontalSpeed = playerSpeed; } // if isAimingMode is false, make the character face the direction they are moving targetVerticalSpeed = (targetVerticalSpeed * movementSpeedOffset) * input_Movement.y; // * Time.deltaTime; targetHorizontalSpeed = (targetHorizontalSpeed * movementSpeedOffset) * input_Movement.x; // * Time.deltaTime; verticalSpeed = Mathf.SmoothDamp(verticalSpeed, targetVerticalSpeed, ref verticalSpeedVelocity, movementSmoothDamp); // makes speed transitions smooth horizontalSpeed = Mathf.SmoothDamp(horizontalSpeed, targetHorizontalSpeed, ref horizontalSpeedVelocity, movementSmoothDamp); // makes speed transitions smooth if (isAimingMode) { } // if isAimingMode is true else { float verticalActualSpeed = verticalSpeed < 0 ? verticalSpeed * -1 : verticalSpeed; // if verticalSpeed is negative, make it positive float horizontalActualSpeed = horizontalSpeed < 0 ? horizontalSpeed * -1 : horizontalSpeed; // if horizontalSpeed is negative, make it positive float animatorVertical = verticalActualSpeed > horizontalActualSpeed ? verticalActualSpeed : horizontalActualSpeed; // set animatorVertical equal to either verticalActualSpeed or horizontalActualSpeed based on which one is greater characterAnimator.SetFloat("Vertical", animatorVertical); // makes the "Vertical" parameter in the characterAnimator equal to animatorVertical //Debug.Log(verticalSpeed); //Debug.Log(animatorVertical); //Debug.Log(input_Movement); //Debug.Log(targetVerticalSpeed); } // if isAimingMode is not true playerMovement = cameraController.transform.forward * verticalSpeed * Time.deltaTime; // incorporates forward movement of playerMovement playerMovement += cameraController.transform.right * horizontalSpeed * Time.deltaTime; // incorporates sideways movement of playerMovement characterController.Move(playerMovement); // moves the characterController based on playerMovement } private void JumpingTimer() { if (jumpingTimer >= 0) // if jumpingTimer is greater than or equal to 0 { jumpingTimer -= Time.deltaTime; // decrease jumpingTimer every second } } // should fire constantly in update private void ToggleWalking() { isWalking = !isWalking; // set isWalking to its opposite value } private void Sprint() { if (isSprinting) // if isSprinting is true { isSprinting = false; return; } // if isSprinting is already true when you press sprint, set isSprinting to false (stop sprinting) if (!CanSprint()) // if CanSprint returns false { return; } // if CanSprint returns false, do nothing if (isAimingMode) { return; } // if isAimingMode is true, do nothing if (stats.Stamina > (stats.MaxStamina / 4)) // if you have more than a quarter of Stamina { isSprinting = true; // set isSprinting to true } // if you have more than a quarter of Stamina, set isSprinting to true } private bool CanSprint() { if (isAimingMode) // if isAimingMode is true { return false; } // if isAimingMode is true, return false var sprintFalloff = .707107f; // the minimum amount of input_Movement the player needs to provide if they want to sprint if((input_Movement.y < 0 ? (input_Movement.y * -1) : input_Movement.y) < sprintFalloff && (input_Movement.x < 0 ? (input_Movement.x * -1) : input_Movement.x) < sprintFalloff) { return false; } // if you aren't providing enough input_Movement, return false return true; // if none of the above if statements are true, return true } private void Jump() // called on press AND release { if(jumpingTimer <= 0) // if jumpingTimer is less than or equal to 0... { jumpingTimer = 0.4f; // set jumpingTimer equal to 0.4 return; // return } Debug.Log("You are jumping."); //Debug.Log(cameraController.transform.forward); } private void SuperJump() { Debug.Log("You are super jumping."); } #region - Enable/Disable - private void OnEnable() { playerInputActions.Enable(); // allows actions from that Input system to be performed } private void OnDisable() { playerInputActions.Disable(); } #endregion }