using System.Collections; using UnityEngine; namespace Creatures.Player.Movement { public class PlayerMove : MonoBehaviour { CharacterController _controller; private TrailRenderer _dashTrail; private PlayerSound _playerSound; private float _footstepTime = 0.3f; public void initialize(CharacterController controller, TrailRenderer dashTrail, PlayerSound playerSound, float footstepTime) { _controller = controller; _dashTrail = dashTrail; _playerSound = playerSound; _footstepTime = footstepTime; } public void ApplyMovement(Vector3 moveDir, Vector3 lookTarget, float moveSpeed) { if (!_dashing) { transform.LookAt(lookTarget); _controller.Move(moveSpeed * moveDir * Time.deltaTime); } } float _lastFootstepTime=0f; Vector3 _lastMoveDir = Vector3.zero; public Vector3 GetMoveDirection(Vector3 input, Transform cameraTransform) { Quaternion camRotation = Quaternion.Euler(0f, cameraTransform.rotation.eulerAngles.y, 0f); input = camRotation * input; if (input != Vector3.zero && _lastFootstepTime+_footstepTime < Time.time) { _lastFootstepTime = Time.time; _playerSound.playFootStepSound(); } _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, bool attacking) { if (sprintPerformed && !attacking && !_dashing && _dashPossible) { _playerSound.playWooshSound(); 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; } } }