113 lines
4.1 KiB
C#
113 lines
4.1 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
[Header("Movement Settings")]
|
|
[SerializeField] private float forwardSpeed = 10f;
|
|
[SerializeField] private float laneDistance = 2.5f; // Расстояние между полосами
|
|
[SerializeField] private float jumpForce = 8f;
|
|
[SerializeField] private float gravity = -20f;
|
|
|
|
[Header("Lanes")]
|
|
[SerializeField] private int currentLane = 1; // 0 - лево, 1 - центр, 2 - право
|
|
private int targetLane = 1;
|
|
private Vector3 targetPosition;
|
|
|
|
[Header("Ground Check")]
|
|
[SerializeField] private Transform groundCheck;
|
|
[SerializeField] private float groundCheckRadius = 0.2f;
|
|
[SerializeField] private LayerMask groundLayer;
|
|
|
|
private CharacterController controller;
|
|
//private Animator animator;
|
|
private Vector3 velocity;
|
|
private bool isGrounded;
|
|
private bool isJumping;
|
|
|
|
void Start()
|
|
{
|
|
controller = GetComponent<CharacterController>();
|
|
//animator = GetComponent<Animator>();
|
|
|
|
// Устанавливаем начальную позицию по центру
|
|
targetPosition = transform.position;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// Проверка земли
|
|
isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundLayer);
|
|
|
|
if (isGrounded && velocity.y < 0)
|
|
{
|
|
velocity.y = -2f; // Небольшая сила притяжения к земле
|
|
if (isJumping)
|
|
{
|
|
isJumping = false;
|
|
//animator.SetBool("isJumping", false);
|
|
}
|
|
}
|
|
|
|
// Автоматическое движение вперёд
|
|
float forwardMovement = forwardSpeed * Time.deltaTime;
|
|
|
|
// Обработка ввода для смены полосы (стрелки или AD)
|
|
if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
|
|
{
|
|
targetLane = Mathf.Min(currentLane + 1, 2);
|
|
}
|
|
else if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
|
|
{
|
|
targetLane = Mathf.Max(currentLane - 1, 0);
|
|
}
|
|
|
|
// Обработка прыжка
|
|
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
|
|
{
|
|
isJumping = true;
|
|
velocity.y = Mathf.Sqrt(jumpForce * -2f * gravity);
|
|
//animator.SetBool("isJumping", true);
|
|
}
|
|
|
|
// Плавно перемещаем целевую позицию по X к нужной полосе
|
|
float targetX = (targetLane - 1) * laneDistance;
|
|
targetPosition.x = Mathf.Lerp(transform.position.x, targetX, Time.deltaTime * 10f);
|
|
|
|
// Вертикальная скорость (гравитация)
|
|
velocity.y += gravity * Time.deltaTime;
|
|
|
|
// Итоговое перемещение
|
|
Vector3 move = new Vector3(
|
|
(targetPosition.x - transform.position.x), // Горизонтальное смещение
|
|
velocity.y * Time.deltaTime, // Вертикальное смещение
|
|
forwardMovement // Движение вперёд
|
|
);
|
|
|
|
controller.Move(move);
|
|
|
|
// Обновляем анимацию бега
|
|
//animator.SetFloat("Speed", forwardSpeed);
|
|
|
|
// Обновляем текущую полосу после перемещения
|
|
if (Mathf.Abs(transform.position.x - targetX) < 0.1f)
|
|
{
|
|
currentLane = targetLane;
|
|
}
|
|
}
|
|
|
|
// Для визуализации проверки земли в редакторе
|
|
void OnDrawGizmosSelected()
|
|
{
|
|
if (groundCheck != null)
|
|
{
|
|
Gizmos.color = Color.red;
|
|
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
|
|
}
|
|
}
|
|
|
|
// Метод для увеличения скорости (будем вызывать из GameManager)
|
|
public void IncreaseSpeed(float amount)
|
|
{
|
|
forwardSpeed += amount;
|
|
}
|
|
} |