temp
This commit is contained in:
1 parent
61af80957c
commit
a570b579b2
9 files changed
+591
-6
No files matched your search
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8aa88306de064a44902a77eeeaeee2fc
|
||||
timeCreated: 1772643921
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 69f2b2331fd2401d9718168564d8cfdf
|
||||
timeCreated: 1772643927
|
||||
@@ -0,0 +1,113 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0e95a5ec640240ca8b461d11cd51653b
|
||||
timeCreated: 1772643933
|
||||
Reference in new issue
Block a user