using System.Collections.Generic; using UnityEngine; using TMPro; public class PlayerMovement : MonoBehaviour { public float moveSpeed = 5f; public float sprintSpeed = 10f; public float gravity = -9.81f; public float rotationSpeed = 10f; public float dashSpeed; public float dashDuration; private CharacterController controller; private Vector3 velocity; private Vector3 move; private PlayerSkillTree PlayerSkills; private PlayerSkillHandler playerSkillHandler; private void Awake() { PlayerSkills = PlayerSkillTree.Instance; playerSkillHandler = GetComponent(); } void Start() { controller = GetComponent(); } private void Update() { if (Input.GetKeyDown(KeyCode.Space) && !playerSkillHandler.IsOnCooldown(PlayerSkillTree.Skills.Dash)) { /* Get the input for horizontal and vertical movement */ float moveX = Input.GetAxisRaw("Horizontal"); float moveZ = Input.GetAxisRaw("Vertical"); move = new Vector3(moveX, 0, moveZ).normalized; StartCoroutine(playerSkillHandler.DashCoroutine(controller, move)); } if (Input.GetKeyDown(KeyCode.L)) { if(PlayerSkills.TryUsePotion(PotionHandler.PotionType.HealthBig)) { Debug.Log("Potion used!"); } else { Debug.Log("Potion not available!"); } } } void FixedUpdate() { HandleMovement(); ApplyGravity(); } void HandleMovement() { float moveX = Input.GetAxisRaw("Horizontal"); float moveZ = Input.GetAxisRaw("Vertical"); move = new Vector3(moveX, 0, moveZ).normalized; if (move.magnitude >= 0.1f) { float currentSpeed = Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : moveSpeed; Quaternion targetRotation = Quaternion.LookRotation(move); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.fixedDeltaTime); controller.Move(move * currentSpeed * Time.fixedDeltaTime); } } void ApplyGravity() { if (IsGrounded() && velocity.y < 0) velocity.y = -2f; velocity.y += gravity * Time.fixedDeltaTime; controller.Move(velocity * Time.fixedDeltaTime); } bool IsGrounded() { float groundDistance = 0.2f; return Physics.Raycast(transform.position, Vector3.down, groundDistance); } public List GetPlayerSkills() { return PlayerSkills.GetPlayerSkills(); } }