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

83 lines
2.2 KiB
C#
Raw Normal View History

2024-12-17 21:46:05 +00:00
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)
2024-12-17 21:46:05 +00:00
{
_controller = controller;
_dashTrail = dashTrail;
2024-12-17 21:46:05 +00:00
}
2024-12-18 14:19:25 +00:00
public void ApplyMovement(Vector3 moveDir, Vector3 lookTarget, float moveSpeed)
2024-12-17 21:46:05 +00:00
{
if (!_dashing)
{
transform.LookAt(lookTarget);
_controller.Move(moveSpeed * moveDir * Time.deltaTime);
}
}
2024-12-18 14:19:25 +00:00
Vector3 _lastMoveDir = Vector3.zero;
public Vector3 GetMoveDirection(Vector3 input, Transform cameraTransform)
2024-12-17 21:46:05 +00:00
{
Quaternion camRotation = Quaternion.Euler(0f, cameraTransform.rotation.eulerAngles.y, 0f);
2024-12-17 21:46:05 +00:00
input = camRotation * input;
2024-12-18 14:19:25 +00:00
_lastMoveDir = input;
2024-12-17 21:46:05 +00:00
return input;
}
public Vector3 Gravity()
2024-12-17 21:46:05 +00:00
{
return Physics.gravity;
}
private bool _dashing = false;
private bool _dashPossible = true;
2024-12-17 21:46:05 +00:00
2024-12-23 20:34:57 +00:00
public void Dash(float dashTime, float dashCooldownTime, float dashSpeed, bool sprintPerformed, bool attacking)
2024-12-17 21:46:05 +00:00
{
2024-12-23 20:34:57 +00:00
if (sprintPerformed && !attacking && !_dashing && _dashPossible)
2024-12-17 21:46:05 +00:00
{
StartCoroutine(DashCoroutine(dashTime, dashCooldownTime, dashSpeed));
2024-12-17 21:46:05 +00:00
}
}
private IEnumerator DashCoroutine(float dashTime, float dashCooldownTime, float dashSpeed)
2024-12-17 21:46:05 +00:00
{
_dashing = true;
_dashPossible = false;
_dashTrail.emitting = true;
2024-12-17 21:46:05 +00:00
float startTime = Time.time;
while(Time.time < startTime + dashTime)
{
2024-12-18 14:19:25 +00:00
_controller.Move(_lastMoveDir * dashSpeed * Time.deltaTime);
2024-12-17 21:46:05 +00:00
yield return null;
}
_dashing = false;
_dashTrail.emitting = false;
2024-12-17 21:46:05 +00:00
StartCoroutine(DashCooldown(dashCooldownTime));
2024-12-17 21:46:05 +00:00
}
private IEnumerator DashCooldown(float dashCooldownTime)
2024-12-17 21:46:05 +00:00
{
float startTime = Time.time;
while(Time.time < startTime + dashCooldownTime)
{
yield return null;
}
_dashPossible = true;
}
}