чё то чё то есть
This commit is contained in:
1 parent
f632508497
commit
f12b4aef0d
14 files changed
+571
-97
No files matched your search
@@ -1,107 +1,249 @@
|
||||
using _Project.Scripts.Utilities;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace _Project.Scripts.PlayerUtils
|
||||
{
|
||||
public class PlayerMovement : MonoSingleton<PlayerMovement>
|
||||
[RequireComponent(typeof(Rigidbody), typeof(CapsuleCollider))]
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
[Header("Камера и Мышь")]
|
||||
[SerializeField] private Transform cameraPosition;
|
||||
[SerializeField] private float mouseSensitivity = 0.1f;
|
||||
[SerializeField] private float maxLookAngle = 80f;
|
||||
[Header("Скорости передвижения (м/с)")]
|
||||
[SerializeField] private float moveSpeed = 6.5f;
|
||||
[SerializeField] private float sprintSpeed = 9.5f;
|
||||
[SerializeField] private float crouchSpeed = 3f;
|
||||
|
||||
[Header("Скорости передвижения (м/с)")]
|
||||
[SerializeField] private float moveSpeed = 6.5f; // Обычный бег (~250 u/s)
|
||||
[SerializeField] private float sprintSpeed = 9.5f; // Спринт (~360 u/s)
|
||||
[SerializeField] private float crouchSpeed = 3f;
|
||||
[Header("Параметры Подката (Slide)")]
|
||||
[SerializeField] private float slideInitialSpeed = 15f;
|
||||
[SerializeField] private float slideDuration = 0.85f;
|
||||
[SerializeField] private float slideFriction = 2.8f;
|
||||
[SerializeField] private float slideCooldown = 0.35f;
|
||||
|
||||
[Header("Параметры Подката (Slide)")]
|
||||
[Tooltip("Начальная взрывная скорость подката")]
|
||||
[SerializeField] private float slideInitialSpeed = 15f; // (~580 u/s)
|
||||
[Tooltip("Длительность скольжения в секундах")]
|
||||
[SerializeField] private float slideDuration = 0.85f;
|
||||
[Tooltip("Трение коленей при подкате")]
|
||||
[SerializeField] private float slideFriction = 2.8f;
|
||||
[Tooltip("Наклон камеры набок при подкате (градусы)")]
|
||||
[SerializeField] private float slideCameraTiltAngle = 4f;
|
||||
[SerializeField] private float slideCooldown = 0.35f;
|
||||
[Header("Физика CS / Source (Bunnyhop & Strafe)")]
|
||||
[SerializeField] private float groundFriction = 5f;
|
||||
[SerializeField] private float groundAcceleration = 10f;
|
||||
[SerializeField] private float airAcceleration = 80f;
|
||||
[SerializeField] private float airCap = 2.0f;
|
||||
[SerializeField] private bool showSpeedometer = true;
|
||||
|
||||
[Header("Физика CS / Source (Bunnyhop & Strafe)")]
|
||||
[Tooltip("Трение на земле (гасит скорость, если не успели прыгнуть вовремя)")]
|
||||
[SerializeField] private float groundFriction = 5f;
|
||||
[Tooltip("Ускорение на земле")]
|
||||
[SerializeField] private float groundAcceleration = 10f;
|
||||
[Tooltip("Сила набора скорости в воздухе при стрейфах (sv_airaccelerate)")]
|
||||
[SerializeField] private float airAcceleration = 80f;
|
||||
[Tooltip("Лимит добавления скорости в воздухе")]
|
||||
[SerializeField] private float airCap = 2.0f;
|
||||
[Tooltip("Показывать спидометр на экране")]
|
||||
[SerializeField] private bool showSpeedometer = true;
|
||||
[Header("Гравитация и Прыжок")]
|
||||
[SerializeField] private float jumpHeight = 1.35f;
|
||||
[SerializeField] private float gravity = -25f;
|
||||
[SerializeField] private float fallMultiplier = 1.8f;
|
||||
[SerializeField] private LayerMask groundLayer;
|
||||
[SerializeField] private float groundCheckDistance = 0.12f;
|
||||
[SerializeField] private float jumpBufferTime = 0.12f;
|
||||
|
||||
[Header("Гравитация и Прыжок")]
|
||||
[SerializeField] private float jumpHeight = 1.35f;
|
||||
[SerializeField] private float gravity = -25f;
|
||||
[SerializeField] private float fallMultiplier = 1.8f;
|
||||
[SerializeField] private LayerMask groundLayer;
|
||||
[SerializeField] private float groundCheckDistance = 0.12f;
|
||||
[Tooltip("Буфер прыжка (время поблажки для колесика мыши)")]
|
||||
[SerializeField] private float jumpBufferTime = 0.12f;
|
||||
[Header("Коллайдер (Приседание)")]
|
||||
[SerializeField] private float standingHeight = 2f;
|
||||
[SerializeField] private float crouchingHeight = 1.05f;
|
||||
[SerializeField] private float crouchTransitionSpeed = 15f;
|
||||
|
||||
[Header("Параметры приседания")]
|
||||
[SerializeField] private float standingHeight = 2f;
|
||||
[SerializeField] private float crouchingHeight = 1.05f;
|
||||
[SerializeField] private float standingCameraY = 1.6f;
|
||||
[SerializeField] private float crouchingCameraY = 0.8f;
|
||||
[SerializeField] private float crouchTransitionSpeed = 15f;
|
||||
|
||||
private Rigidbody rb;
|
||||
private CapsuleCollider col;
|
||||
private Rigidbody rb;
|
||||
private CapsuleCollider col;
|
||||
private PlayerControls controls;
|
||||
|
||||
private Vector2 moveInput;
|
||||
private Vector2 lookInput;
|
||||
private Vector3 horizontalVelocity = Vector3.zero;
|
||||
private float verticalVelocity = 0f;
|
||||
|
||||
private bool isGrounded;
|
||||
private bool wasGrounded;
|
||||
private bool isSprinting = false;
|
||||
private bool isCrouching = false;
|
||||
private float jumpBufferTimer = 0f;
|
||||
public bool IsGrounded { get; private set; }
|
||||
public bool IsSprinting { get; private set; }
|
||||
public bool IsCrouching { get; private set; }
|
||||
public bool IsSliding { get; private set; }
|
||||
public Vector2 MoveInput { get; private set; }
|
||||
public float CurrentSpeed => horizontalVelocity.magnitude;
|
||||
private bool wasGrounded;
|
||||
private float jumpBufferTimer = 0f;
|
||||
private Vector3 horizontalVelocity = Vector3.zero;
|
||||
private float verticalVelocity = 0f;
|
||||
|
||||
// Подкат
|
||||
private bool isSliding = false;
|
||||
private float slideTimer = 0f;
|
||||
private float slideCurrentSpeed = 0f;
|
||||
private Vector3 slideDirection = Vector3.zero;
|
||||
private float slideCooldownTimer = 0f;
|
||||
|
||||
private PlayerControls controls;
|
||||
|
||||
void Awake()
|
||||
private float slideTimer = 0f;
|
||||
private float slideCurrentSpeed = 0f;
|
||||
private Vector3 slideDirection = Vector3.zero;
|
||||
private float slideCooldownTimer = 0f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
rb = GetComponent<Rigidbody>();
|
||||
col = GetComponent<CapsuleCollider>();
|
||||
mouseSensitivity = PlayerPrefs.GetFloat("MouseSensitivity", mouseSensitivity);
|
||||
|
||||
rb.useGravity = false;
|
||||
rb.freezeRotation = true;
|
||||
rb.interpolation = RigidbodyInterpolation.None;
|
||||
|
||||
controls = Player.Instance.Controls;
|
||||
|
||||
controls.Player.Sprint.started += _ => IsSprinting = true;
|
||||
controls.Player.Sprint.canceled += _ => IsSprinting = false;
|
||||
|
||||
controls = new PlayerControls();
|
||||
|
||||
controls = Player.Instance.controls;
|
||||
// Спринт
|
||||
controls.Player.Sprint.started += ctx => isSprinting = true;
|
||||
controls.Player.Sprint.canceled += ctx => isSprinting = false;
|
||||
|
||||
/* // Присед и Подкат
|
||||
controls.Player.Crouch.started += ctx => OnCrouchPressed();
|
||||
controls.Player.Crouch.canceled += ctx => OnCrouchReleased();
|
||||
|
||||
// Прыжок на Пробел (через буфер)
|
||||
controls.Player.Jump.performed += ctx => QueueJump();
|
||||
*/
|
||||
controls.Player.Crouch.started += _ => OnCrouchPressed();
|
||||
controls.Player.Crouch.canceled += _ => OnCrouchReleased();
|
||||
|
||||
controls.Player.Jump.performed += _ => QueueJump();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
MoveInput = controls.Player.Move.ReadValue<Vector2>();
|
||||
|
||||
if (jumpBufferTimer > 0f) jumpBufferTimer -= Time.deltaTime;
|
||||
if (slideCooldownTimer > 0f) slideCooldownTimer -= Time.deltaTime;
|
||||
|
||||
if (Mouse.current != null && Mathf.Abs(Mouse.current.scroll.y.ReadValue()) > 0.1f)
|
||||
QueueJump();
|
||||
|
||||
HandleColliderHeight();
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
CheckGrounded();
|
||||
CalculateCustomGravity();
|
||||
|
||||
if (jumpBufferTimer > 0f && IsGrounded)
|
||||
ExecuteJump();
|
||||
|
||||
ApplyMovement();
|
||||
|
||||
rb.linearVelocity = new Vector3(horizontalVelocity.x, verticalVelocity, horizontalVelocity.z);
|
||||
wasGrounded = IsGrounded;
|
||||
}
|
||||
|
||||
private void HandleColliderHeight()
|
||||
{
|
||||
bool isLowered = IsCrouching || IsSliding;
|
||||
float targetHeight = isLowered ? crouchingHeight : standingHeight;
|
||||
col.height = Mathf.Lerp(col.height, targetHeight, Time.deltaTime * crouchTransitionSpeed);
|
||||
col.center = new Vector3(col.center.x, col.height * 0.5f, col.center.z);
|
||||
}
|
||||
|
||||
private void QueueJump() => jumpBufferTimer = jumpBufferTime;
|
||||
|
||||
private void ExecuteJump()
|
||||
{
|
||||
if (IsSliding)
|
||||
{
|
||||
horizontalVelocity = slideDirection * slideCurrentSpeed;
|
||||
StopSlide();
|
||||
}
|
||||
|
||||
verticalVelocity = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
||||
jumpBufferTimer = 0f;
|
||||
|
||||
Player.Instance.CameraController.TriggerJumpImpact();
|
||||
|
||||
// ВЫЗОВ ЗВУКА ИЗ НОВОГО МОДУЛЯ
|
||||
Player.Instance.Sound.PlayJump();
|
||||
}
|
||||
|
||||
private void ApplyMovement()
|
||||
{
|
||||
if (IsSliding)
|
||||
{
|
||||
slideCurrentSpeed = Mathf.Lerp(slideCurrentSpeed, crouchSpeed, Time.fixedDeltaTime * slideFriction);
|
||||
slideTimer -= Time.fixedDeltaTime;
|
||||
|
||||
Vector3 steerDir = (transform.forward * MoveInput.y + transform.right * MoveInput.x).normalized;
|
||||
if (steerDir.sqrMagnitude > 0.01f)
|
||||
slideDirection = Vector3.Slerp(slideDirection, steerDir, Time.fixedDeltaTime * 2.5f);
|
||||
|
||||
horizontalVelocity = slideDirection * slideCurrentSpeed;
|
||||
|
||||
if (slideTimer <= 0f || slideCurrentSpeed <= crouchSpeed + 0.5f)
|
||||
{
|
||||
StopSlide();
|
||||
IsCrouching = controls.Player.Crouch.IsPressed();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 wishDir = (transform.forward * MoveInput.y + transform.right * MoveInput.x);
|
||||
wishDir.y = 0f;
|
||||
wishDir.Normalize();
|
||||
|
||||
float targetSpeed = IsCrouching ? crouchSpeed : (IsSprinting ? sprintSpeed : moveSpeed);
|
||||
|
||||
if (IsGrounded)
|
||||
{
|
||||
if (jumpBufferTimer <= 0f) ApplyGroundFriction();
|
||||
Accelerate(wishDir, targetSpeed, groundAcceleration);
|
||||
}
|
||||
else
|
||||
{
|
||||
AirAccelerate(wishDir, targetSpeed, airAcceleration, airCap);
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyGroundFriction()
|
||||
{
|
||||
float speed = horizontalVelocity.magnitude;
|
||||
if (speed < 0.0001f) { horizontalVelocity = Vector3.zero; return; }
|
||||
float drop = speed * groundFriction * Time.fixedDeltaTime;
|
||||
horizontalVelocity *= (Mathf.Max(speed - drop, 0f) / speed);
|
||||
}
|
||||
|
||||
private void Accelerate(Vector3 wishDir, float wishSpeed, float accel)
|
||||
{
|
||||
float currentSpeed = Vector3.Dot(horizontalVelocity, wishDir);
|
||||
float addSpeed = Mathf.Max(wishSpeed - currentSpeed, 0f);
|
||||
horizontalVelocity += wishDir * Mathf.Min(accel * wishSpeed * Time.fixedDeltaTime, addSpeed);
|
||||
}
|
||||
|
||||
private void AirAccelerate(Vector3 wishDir, float wishSpeed, float accel, float cap)
|
||||
{
|
||||
float currentSpeed = Vector3.Dot(horizontalVelocity, wishDir);
|
||||
float addSpeed = Mathf.Max(Mathf.Min(wishSpeed, cap) - currentSpeed, 0f);
|
||||
horizontalVelocity += wishDir * Mathf.Min(accel * wishSpeed * Time.fixedDeltaTime, addSpeed);
|
||||
}
|
||||
|
||||
private void CheckGrounded()
|
||||
{
|
||||
float checkRadius = col.radius * 0.85f;
|
||||
float bottomOffset = (col.height * 0.5f) - checkRadius;
|
||||
Vector3 rayOrigin = transform.position + col.center - (Vector3.up * bottomOffset) + (Vector3.up * 0.05f);
|
||||
|
||||
IsGrounded = Physics.SphereCast(rayOrigin, checkRadius, Vector3.down, out _, groundCheckDistance + 0.05f, groundLayer, QueryTriggerInteraction.Ignore);
|
||||
|
||||
if (!wasGrounded && IsGrounded)
|
||||
{
|
||||
// ВЫЗОВ ЗВУКА ИЗ НОВОГО МОДУЛЯ
|
||||
Player.Instance.Sound.PlayLand();
|
||||
}
|
||||
|
||||
if (IsGrounded && jumpBufferTimer <= 0f && rb.linearVelocity.magnitude > 0.01f && horizontalVelocity == Vector3.zero)
|
||||
horizontalVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
|
||||
}
|
||||
|
||||
private void CalculateCustomGravity()
|
||||
{
|
||||
if (IsGrounded && verticalVelocity < 0f) verticalVelocity = -2f;
|
||||
else verticalVelocity += ((verticalVelocity < 0f) ? (gravity * fallMultiplier) : gravity) * Time.fixedDeltaTime;
|
||||
}
|
||||
|
||||
private void OnCrouchPressed()
|
||||
{
|
||||
if (IsSprinting && IsGrounded && MoveInput.y > 0.2f && slideCooldownTimer <= 0f) StartSlide();
|
||||
else IsCrouching = true;
|
||||
}
|
||||
|
||||
private void OnCrouchReleased()
|
||||
{
|
||||
IsCrouching = false;
|
||||
if (IsSliding) StopSlide();
|
||||
}
|
||||
|
||||
private void StartSlide()
|
||||
{
|
||||
IsSliding = true;
|
||||
slideTimer = slideDuration;
|
||||
slideCurrentSpeed = slideInitialSpeed;
|
||||
slideDirection = (transform.forward * MoveInput.y + transform.right * MoveInput.x).normalized;
|
||||
if (slideDirection.sqrMagnitude < 0.01f) slideDirection = transform.forward;
|
||||
|
||||
// ВЫЗОВ ЗВУКА ИЗ НОВОГО МОДУЛЯ
|
||||
Player.Instance.Sound.PlaySlide();
|
||||
}
|
||||
|
||||
private void StopSlide()
|
||||
{
|
||||
IsSliding = false;
|
||||
slideCooldownTimer = slideCooldown;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user