49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem; // Dodaliœmy to!
|
|
|
|
public class PlayerMove : MonoBehaviour
|
|
{
|
|
[Header("Ustawienia ruchu")]
|
|
public float speed = 5f;
|
|
|
|
[Header("Grawitacja")]
|
|
public float gravity = -9.81f; // WartoϾ grawitacji
|
|
|
|
private CharacterController controller;
|
|
private Vector3 velocity; // Przechowuje prêdkoœæ w osi Y (spadanie)
|
|
|
|
void Start() => controller = GetComponent<CharacterController>();
|
|
|
|
void Update()
|
|
{
|
|
// --- 1. SPRAWDZANIE POD£O¯A ---
|
|
// isGrounded to wbudowana funkcja, która sprawdza czy dolna czêœæ Capsule Collidera dotyka ziemi
|
|
if (controller.isGrounded && velocity.y < 0)
|
|
{
|
|
// Lekko dociskamy postaæ do ziemi, ¿eby nie œlizga³a siê na schodach/zboczach
|
|
velocity.y = -2f;
|
|
}
|
|
|
|
// --- 2. NOWY SPOSÓB POBIERANIA WASD ---
|
|
Vector2 moveInput = Vector2.zero;
|
|
|
|
if (Keyboard.current != null)
|
|
{
|
|
if (Keyboard.current.wKey.isPressed) moveInput.y = 1;
|
|
if (Keyboard.current.sKey.isPressed) moveInput.y = -1;
|
|
if (Keyboard.current.aKey.isPressed) moveInput.x = -1;
|
|
if (Keyboard.current.dKey.isPressed) moveInput.x = 1;
|
|
}
|
|
|
|
// --- 3. RUCH W POZIOMIE (Chodzenie) ---
|
|
Vector3 move = transform.right * moveInput.x + transform.forward * moveInput.y;
|
|
controller.Move(move * speed * Time.deltaTime);
|
|
|
|
// --- 4. RUCH W PIONIE (Spadanie) ---
|
|
// Aplikujemy przyspieszenie grawitacyjne do naszej prêdkoœci spadania
|
|
velocity.y += gravity * Time.deltaTime;
|
|
|
|
// Poruszamy postaci¹ w dó³
|
|
controller.Move(velocity * Time.deltaTime);
|
|
}
|
|
} |