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