Сделал базовое управление машиной и персонажем

This commit is contained in:
bybohka committed 2026-09-19 16:56:59 +03:00
1 parent 10d42b72b8
commit 48753f65fb
152 files changed
+15117 -174

No files matched your search

@@ -0,0 +1,39 @@
using UnityEngine;
public class PlayerInputHandler : MonoBehaviour
{
[Header("Components")]
[SerializeField] private PlayerMovement playerMovement;
[SerializeField] private PlayerLook playerLook;
private void Update()
{
HandleMovementInput();
HandleLookInput();
}
private void HandleMovementInput()
{
// Читаем оси WASD. GetAxisRaw дает более резкое, отзывчивое управление без инерции.
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
// Нормализуем вектор, чтобы игрок не двигался быстрее по диагонали
Vector2 moveInput = new Vector2(horizontal, vertical).normalized;
if (playerMovement != null)
playerMovement.ProcessMove(moveInput);
}
private void HandleLookInput()
{
// Читаем движение мыши
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
Vector2 lookInput = new Vector2(mouseX, mouseY);
if (playerLook != null)
playerLook.ProcessLook(lookInput);
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 00826943886c407c952993731de997e0
timeCreated: 1789818508
@@ -0,0 +1,33 @@
using UnityEngine;
public class PlayerLook : MonoBehaviour
{
[Header("Look Settings")]
[SerializeField] private float mouseSensitivity = 150f;
[SerializeField] private Transform playerBody;
private float xRotation = 0f;
private void Start()
{
// Блокируем курсор в центре экрана и скрываем его
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
public void ProcessLook(Vector2 input)
{
float mouseX = input.x * mouseSensitivity * Time.deltaTime;
float mouseY = input.y * mouseSensitivity * Time.deltaTime;
// Расчет наклона камеры с ограничением, чтобы нельзя было сломать шею
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
// Применяем вращение камеры (вверх/вниз)
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
// Вращаем само тело игрока (влево/вправо)
playerBody.Rotate(Vector3.up * mouseX);
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4662192a51344c429f6fb8cb1730a7e2
timeCreated: 1789818561
@@ -0,0 +1,42 @@
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour
{
[Header("Movement Settings")]
[SerializeField] private float moveSpeed = 4f;
[SerializeField] private float gravity = -9.81f;
[Header("Ground Check")]
[SerializeField] private Transform groundCheck;
[SerializeField] private float groundDistance = 0.4f;
[SerializeField] private LayerMask groundMask;
private CharacterController controller;
private Vector3 velocity;
private bool isGrounded;
private void Awake()
{
controller = GetComponent<CharacterController>();
}
public void ProcessMove(Vector2 input)
{
// Проверка касания земли (сфера на уровне ног)
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f; // Небольшое прижатие к земле для стабильного спуска по склонам
}
// Движение относительно локальных координат игрока
Vector3 move = transform.right * input.x + transform.forward * input.y;
controller.Move(move * (moveSpeed * Time.deltaTime));
// Применение гравитации
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bf210e4b179e4c4a93ffda4241a24602
timeCreated: 1789818546