twinStick/twinStickCrawler/Assets/Scripts/Creatures/Player/Movement/PlayerMove.cs

94 lines
2.9 KiB
C#
Raw Normal View History

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