using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.Serialization; public class PlayerMove : MonoBehaviour { CharacterController _controller; private TrailRenderer _dashTrail; public void initialize(CharacterController controller, TrailRenderer dashTrail) { _controller = controller; _dashTrail = dashTrail; } public void ApplyMovement(Vector3 moveDir, Vector3 lookTarget, float moveSpeed) { if (!_dashing) { transform.LookAt(lookTarget); _controller.Move(moveSpeed * moveDir * Time.deltaTime); } } Vector3 _lastMoveDir = Vector3.zero; public Vector3 GetMoveDirection(Vector3 input, Transform cameraTransform) { Quaternion camRotation = Quaternion.Euler(0f, cameraTransform.rotation.eulerAngles.y, 0f); input = camRotation * input; _lastMoveDir = input; return input; } public Vector3 Gravity() { return Physics.gravity; } private bool _dashing = false; private bool _dashPossible = true; public void Dash(float dashTime, float dashCooldownTime, float dashSpeed, bool sprintPerformed) { if (sprintPerformed && !_dashing && _dashPossible) { StartCoroutine(DashCoroutine(dashTime, dashCooldownTime, dashSpeed)); } } private IEnumerator DashCoroutine(float dashTime, float dashCooldownTime, float dashSpeed) { _dashing = true; _dashPossible = false; _dashTrail.emitting = true; float startTime = Time.time; while(Time.time < startTime + dashTime) { _controller.Move(_lastMoveDir * dashSpeed * Time.deltaTime); yield return null; } _dashing = false; _dashTrail.emitting = false; StartCoroutine(DashCooldown(dashCooldownTime)); } private IEnumerator DashCooldown(float dashCooldownTime) { float startTime = Time.time; while(Time.time < startTime + dashCooldownTime) { yield return null; } _dashPossible = true; } }