using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public float speed = 5f;
public float jumpForce = 7f;
private Rigidbody2D rb;
private float movement;
private bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
// Движение
movement = 0f;
if (Keyboard.current.aKey.isPressed) movement = -1f;
if (Keyboard.current.dKey.isPressed) movement = 1f;
// Прыжок
if (Keyboard.current.spaceKey.wasPressedThisFrame && isGrounded)
{
rb.linearVelocity = new Vector2(rb.linearVelocity.x, jumpForce);
}
}
void FixedUpdate()
{
rb.linearVelocity = new Vector2(movement * speed, rb.linearVelocity.y);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
}