using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(CharacterController))] [RequireComponent(typeof(SpriteRenderer))] [RequireComponent(typeof(Animator))] public class BasicPlayerMovement : MonoBehaviour { CharacterController controller; SpriteRenderer spriteRenderer; Animator animator; float verticalVelocity; float gravity = 70.0f; float jumpDivisor = 1.3f; float buttonCooldown; float turnDirectionPlaceholder; float turnDirection = 1; // Temp bool isRunning; bool shouldStopRunning; void Start() { controller = GetComponent(); if (controller == null) { Debug.LogError("BasicPlayerMovement::CharacterController is NULL!"); } spriteRenderer = GetComponent(); if (spriteRenderer == null) { Debug.LogError("BasicPlayerMovement::SpriteRenderer is NULL!"); } animator = GetComponent(); if (animator == null) { Debug.LogError("BasicPlayerMovement::Animator is NULL!"); } } // Public turn direction public float TurnDirection() { return turnDirection; } // Use in the player Update() method public void CalculateMovement( float xSpeed = 7.0f, float xRunSpeedMultipler = 2, float zSpeed = 7.0f, float jumpForce = 30.0f ) { float x = Input.GetAxisRaw("Horizontal"); float z = Input.GetAxisRaw("Vertical"); Vector3 move; DirectionController(x); JumpController(isRunning ? jumpForce / jumpDivisor : jumpForce); RunController(x); AnimationController(); if (!controller.isGrounded) { if (isRunning) { move = new Vector3(turnDirectionPlaceholder * xSpeed * xRunSpeedMultipler, verticalVelocity, z * zSpeed); } else { move = new Vector3(turnDirectionPlaceholder * xSpeed, verticalVelocity, z * zSpeed); } } else { if (isRunning) { move = new Vector3(turnDirectionPlaceholder * xSpeed * xRunSpeedMultipler, verticalVelocity, z * zSpeed); } else { move = new Vector3(x * xSpeed, verticalVelocity, z * zSpeed); } } controller.Move(move * Time.deltaTime); } void AnimationController() { if (isRunning) { animator.SetBool("IsRunning", true); } else { animator.SetBool("IsRunning", false); } if (controller.isGrounded) { animator.SetBool("IsJumping", false); } else { animator.SetBool("IsJumping", true); } } void JumpController(float jumpForce) { if (controller.isGrounded) { // Gravity verticalVelocity = -gravity * Time.deltaTime; if (Input.GetKeyDown(KeyCode.Space)) { // Jump verticalVelocity = jumpForce; } } else { // Gravity affecting the object while jump verticalVelocity -= gravity * Time.deltaTime; } } void RunController(float x) { if (Input.GetButtonDown("Horizontal")) { // Set flag to stop running after direction change while jumping if (turnDirection != x && !controller.isGrounded) { shouldStopRunning = true; } // Stop running after direction change on the ground if (turnDirection != x && controller.isGrounded) { isRunning = false; } // Check if previously pressed button is the same than current to set running if (buttonCooldown > 0 && turnDirection == x) { isRunning = true; } else { // Set coldown after first button press buttonCooldown = 0.2f; // Store direction to check after the second press turnDirection = x; } } // Count remaining cooldown for second button press if (buttonCooldown > 0) { buttonCooldown -= 1 * Time.deltaTime; } // Stop running after landing if the flag is enabled if (shouldStopRunning && controller.isGrounded) { isRunning = false; shouldStopRunning = false; } } void DirectionController(float x) { if (x < 0) { spriteRenderer.flipX = true; if (controller.isGrounded) { turnDirectionPlaceholder = -1.0f; } return; } if (x > 0) { spriteRenderer.flipX = false; if (controller.isGrounded) { turnDirectionPlaceholder = 1.0f; } return; } // Prevent from unexpected stop moving if (controller.isGrounded && !isRunning) { turnDirectionPlaceholder = 0; } } }