basic player controller
This commit is contained in:
1 parent
a570b579b2
commit
7af7baf7f0
2 files changed
+43
-121
No files matched your search
@@ -2,110 +2,70 @@ using UnityEngine;
|
||||
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[Header("Movement Settings")]
|
||||
[SerializeField] private float forwardSpeed = 10f;
|
||||
[SerializeField] private float laneDistance = 2.5f; // Расстояние между полосами
|
||||
[SerializeField] private float laneDistance = 3.0f;
|
||||
[SerializeField] private float jumpForce = 8f;
|
||||
[SerializeField] private float gravity = -20f;
|
||||
[SerializeField] private float gravity = 20f;
|
||||
|
||||
[Header("Lanes")]
|
||||
[SerializeField] private int currentLane = 1; // 0 - лево, 1 - центр, 2 - право
|
||||
private int targetLane = 1;
|
||||
private Vector3 targetPosition;
|
||||
private int _targetLane = 1;
|
||||
|
||||
[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;
|
||||
private CharacterController _controller;
|
||||
private Vector3 _direction;
|
||||
|
||||
void Start()
|
||||
private void Start()
|
||||
{
|
||||
controller = GetComponent<CharacterController>();
|
||||
//animator = GetComponent<Animator>();
|
||||
|
||||
// Устанавливаем начальную позицию по центру
|
||||
targetPosition = transform.position;
|
||||
_controller = GetComponent<CharacterController>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
private 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);
|
||||
_targetLane = Mathf.Min(_targetLane + 1, 2);
|
||||
}
|
||||
else if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
|
||||
{
|
||||
targetLane = Mathf.Max(currentLane - 1, 0);
|
||||
_targetLane = Mathf.Max(_targetLane - 1, 0);
|
||||
}
|
||||
|
||||
// Обработка прыжка
|
||||
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
|
||||
var targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;
|
||||
|
||||
switch (_targetLane)
|
||||
{
|
||||
isJumping = true;
|
||||
velocity.y = Mathf.Sqrt(jumpForce * -2f * gravity);
|
||||
//animator.SetBool("isJumping", true);
|
||||
case 0:
|
||||
targetPosition += Vector3.left * laneDistance;
|
||||
break;
|
||||
case 2:
|
||||
targetPosition += Vector3.right * laneDistance;
|
||||
break;
|
||||
}
|
||||
|
||||
// Плавно перемещаем целевую позицию по 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)
|
||||
if (_controller.isGrounded)
|
||||
{
|
||||
currentLane = targetLane;
|
||||
_direction.y = -1f;
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Space))
|
||||
{
|
||||
_direction.y = jumpForce;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_direction.y -= gravity * Time.deltaTime;
|
||||
}
|
||||
|
||||
_direction.z = forwardSpeed;
|
||||
|
||||
var diff = targetPosition - transform.position;
|
||||
var moveDir = diff.normalized * (25f * Time.deltaTime);
|
||||
|
||||
var moveX = Mathf.Clamp(diff.x, -moveDir.magnitude, moveDir.magnitude);
|
||||
|
||||
var finalMove = new Vector3(moveX, _direction.y * Time.deltaTime, _direction.z * Time.deltaTime);
|
||||
|
||||
_controller.Move(finalMove);
|
||||
}
|
||||
|
||||
// Для визуализации проверки земли в редакторе
|
||||
void OnDrawGizmosSelected()
|
||||
{
|
||||
if (groundCheck != null)
|
||||
{
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
|
||||
}
|
||||
}
|
||||
|
||||
// Метод для увеличения скорости (будем вызывать из GameManager)
|
||||
public void IncreaseSpeed(float amount)
|
||||
{
|
||||
forwardSpeed += amount;
|
||||
|
||||
@@ -335,37 +335,6 @@ MonoBehaviour:
|
||||
m_VarianceClampScale: 0.9
|
||||
m_ContrastAdaptiveSharpening: 0
|
||||
m_Version: 2
|
||||
--- !u!1 &680819701
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 680819702}
|
||||
m_Layer: 0
|
||||
m_Name: GroundChecker
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!4 &680819702
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 680819701}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: -1, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
m_Father: {fileID: 1630184751}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1 &832575517
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -698,7 +667,6 @@ Transform:
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children:
|
||||
- {fileID: 680819702}
|
||||
- {fileID: 330585546}
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
@@ -716,14 +684,8 @@ MonoBehaviour:
|
||||
m_EditorClassIdentifier: Assembly-CSharp::Code.Gameplay.Player.PlayerController
|
||||
forwardSpeed: 10
|
||||
laneDistance: 2.5
|
||||
jumpForce: 8
|
||||
gravity: -2
|
||||
currentLane: 1
|
||||
groundCheck: {fileID: 680819702}
|
||||
groundCheckRadius: 0.2
|
||||
groundLayer:
|
||||
serializedVersion: 2
|
||||
m_Bits: 4294967295
|
||||
jumpForce: 25
|
||||
gravity: 20
|
||||
--- !u!1 &1639123675
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
Reference in new issue
Block a user