UnityGame/Assets/Scripts/PlayerMovement.cs
2024-10-27 10:53:47 +03:00

220 lines
7.4 KiB
C#

using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Movement Settings")]
public float mouseSensitivity = 2.0f;
[Header("Movement Speeds")]
public float walkSpeed = 1.4f; // Ñêîðîñòü õîäüáû
public float runSpeed = 2.5f; // Ñêîðîñòü áåãà
public float crouchSpeed = 1.1f; // Ñêîðîñòü ïðè ïðèñåäàíèè
private float current_speed;
[Header("Camera Settings")]
public Camera playerCamera; // Ïðèâÿæèòå êàìåðó ÷åðåç Inspector
[Header("Head Bobbing Settings")]
public float bobFrequency = 1.5f; // ×àñòîòà òðÿñêè
public float bobHorizontalAmplitude = 0.1f; // Ãîðèçîíòàëüíàÿ àìïëèòóäà
public float bobVerticalAmplitude = 0.1f; // Âåðòèêàëüíàÿ àìïëèòóäà
public Vector3 originalCameraPosition;
private float bobTimer = 1f;
[Header("Acceleration Settings")]
public float acceleration = 14f;
public float deceleration = 10f;
private Vector3 currentVelocity = Vector3.zero;
[Header("Jump Settings")]
public float jumpHeight = 1.52f;
public float gravity = -30f;
private float verticalVelocity = 0f;
[Header("Crouch Settings")]
public float crouchHeight = 1f;
public float standingHeight = 2f;
private bool isCrouching = false;
private CharacterController controller;
private float rotationX = 0f;
private float rotationY = 0f;
[Header("Camera Shake Settings")]
public float shakeIntensity = 1f; // Èíòåíñèâíîñòü âðàùåíèÿ
public float shakeFrequency = 2.0f; // ×àñòîòà âðàùåíèÿ
public float restShakeFrequency = 0.5f; // ×àñòîòà âðàùåíèÿ â ñîñòîÿíèè ïîêîÿ
public float restShakeIntensity = 0.5f; // Èíòåíñèâíîñòü âðàùåíèÿ â ñîñòîÿíèè ïîêîÿ
void Start()
{
controller = GetComponent<CharacterController>();
// Ñêðûòü è çàáëîêèðîâàòü êóðñîð
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
// Åñëè êàìåðà íå íàçíà÷åíà, ïûòàåìñÿ íàéòè åå ñðåäè äî÷åðíèõ îáúåêòîâ
if (playerCamera == null)
{
playerCamera = GetComponentInChildren<Camera>();
}
if (playerCamera == null)
{
Debug.LogError("PlayerMovement: Êàìåðà íå íàçíà÷åíà è íå íàéäåíà ñðåäè äî÷åðíèõ îáúåêòîâ.");
}
originalCameraPosition = playerCamera.transform.localPosition; // Ñîõðàíÿåì èñõîäíóþ ïîçèöèþ êàìåðû
}
void Update()
{
if (playerCamera == null)
{
Debug.LogWarning("PlayerMovement: Êàìåðà íå íàçíà÷åíà.");
return;
}
// === Óïðàâëåíèå êàìåðîé ===
// Ïîëó÷åíèå ââîäà ìûøè
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
rotationY += mouseX;
rotationX -= mouseY;
rotationX = Mathf.Clamp(rotationX, -90f, 90f);
// Ïðèìåíåíèå âðàùåíèÿ ê êàìåðå
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0f, 0f);
// Ïîâîðîò èãðîêà ïî îñè Y
transform.rotation = Quaternion.Euler(0f, rotationY, 0f);
// === Óïðàâëåíèå äâèæåíèåì ===
// Îáðàáîòêà ïðèñåäàíèÿ
if (Input.GetKeyDown(KeyCode.LeftControl))
{
Crouch();
}
else if (Input.GetKeyUp(KeyCode.LeftControl))
{
StandUp();
}
// Ïîëó÷åíèå ââîäà äâèæåíèÿ
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
bool isMoving = moveHorizontal != 0 || moveVertical != 0;
// Îïðåäåëÿåì òåêóùóþ ñêîðîñòü
float targetSpeed = walkSpeed;
if (Input.GetKey(KeyCode.LeftShift) && !isCrouching)
{
targetSpeed = runSpeed;
}
if (isCrouching)
{
targetSpeed = crouchSpeed;
}
Vector3 moveDirection = transform.right * moveHorizontal + transform.forward * moveVertical;
Vector3 desiredVelocity = moveDirection.normalized * targetSpeed;
// Ïëàâíîå èçìåíåíèå ñêîðîñòè
if (isMoving)
{
currentVelocity = Vector3.MoveTowards(currentVelocity, desiredVelocity, acceleration * Time.deltaTime);
}
else
{
currentVelocity = Vector3.MoveTowards(currentVelocity, Vector3.zero, deceleration * Time.deltaTime);
}
// === Ïðûæîê è ãðàâèòàöèÿ ===
if (controller.isGrounded)
{
// Ñáðàñûâàåì âåðòèêàëüíóþ ñêîðîñòü, åñëè íà çåìëå
if (verticalVelocity < 0)
{
verticalVelocity = -2f;
}
// Ïðîâåðÿåì íàæàòèå êëàâèøè ïðûæêà
if (Input.GetButtonDown("Jump") && !isCrouching)
{
verticalVelocity = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
}
else
{
// Ïðèìåíÿåì ãðàâèòàöèþ
verticalVelocity += gravity * Time.deltaTime;
}
// Äîáàâëÿåì âåðòèêàëüíóþ ñêîðîñòü ê äâèæåíèþ
Vector3 move = currentVelocity;
move.y = verticalVelocity;
float bobOffsetY = 0.9f;
float bobOffsetX = 0f;
controller.Move(move * Time.deltaTime);
// if (controller.isGrounded && !isMoving) //ïîêîé
// {
// bobOffsetY = Mathf.Sin(Time.time * restShakeFrequency) * restShakeIntensity * 0.5f;
// bobOffsetX = Mathf.Cos(Time.time * restShakeFrequency * 0.2f) * restShakeIntensity * 0.1f;
// playerCamera.transform.localPosition = originalCameraPosition + new Vector3(bobOffsetX, bobOffsetX, 0f);
// // Òðÿñêà êàìåðû ÷åðåç âðàùåíèå
// float shakeRotationX = Mathf.Sin(Time.time * shakeFrequency * 0.08f) * shakeIntensity;
// float shakeRotationY = Mathf.Cos(Time.time * shakeFrequency * 0.2f) * shakeIntensity * 3;
// playerCamera.transform.localRotation *= Quaternion.Euler(shakeRotationX, 0f, shakeRotationY); // Ïðèìåíÿåì òðÿñêó ÷åðåç âðàùåíèå
// }
// // === Òðÿñêà êàìåðû è ïîêà÷èâàíèÿ ===
// if (controller.isGrounded && isMoving)
// {
// bobTimer += Time.deltaTime * 0.1f * targetSpeed;
// bobOffsetY = Mathf.Sin(bobTimer * 0.2f) * bobVerticalAmplitude;
// bobOffsetX = Mathf.Cos(bobTimer * 0.5f) * Mathf.Cos(Time.time * restShakeFrequency * 0.2f) * restShakeIntensity * 0.1f; ;
// playerCamera.transform.localPosition = originalCameraPosition + new Vector3(bobOffsetX, bobOffsetY, 0f);
// // Òðÿñêà êàìåðû ÷åðåç âðàùåíèå
// float shakeRotationX = Mathf.Sin(Time.time * shakeFrequency * 0.5f) * shakeIntensity * targetSpeed;
// float shakeRotationY = Mathf.Cos(Time.time * shakeFrequency) * shakeIntensity * targetSpeed / 3;
// playerCamera.transform.localRotation *= Quaternion.Euler(shakeRotationX, 0f, shakeRotationY); // Ïðèìåíÿåì òðÿñêó ÷åðåç âðàùåíèå
// }
// else
// {
// // Ñáðîñ òðÿñêè, êîãäà èãðîê íå äâèæåòñÿ
// bobTimer = 0.1f;
// playerCamera.transform.localPosition = Vector3.Lerp(playerCamera.transform.localPosition, originalCameraPosition, Time.deltaTime * 1f);
// }
}
void Crouch()
{
controller.height = crouchHeight;
isCrouching = true;
}
void StandUp()
{
controller.height = standingHeight;
isCrouching = false;
}
}