[SerializeField] private float speed = 3f;
[SerializeField] private int lives = 5;
[SerializeField] private float jumpForce = 7f;
private bool isGrounded = false;
private Rigidbody2D rb;
private SpriteRenderer sprite;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
sprite = GetComponentInChildren<SpriteRenderer>();
}
private void FixedUpdate()
{
checkGround();
}
private void Update()
{
if (Input.GetButton("Horizontal"))
{
Run();
}
if (isGrounded && Input.GetButtonDown("Jump"))
{
Jump();
}
}
private void Run()
{
Vector3 dir = transform.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards(transform.position, transform.position + dir, speed * Time.deltaTime);
sprite.flipX = dir.x < 0.0f;
}
private void Jump()
{
rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
private void checkGround()
{
Collider2D[] collider = Physics2D.OverlapCircleAll(transform.position, 0.3f);
isGrounded = collider.Length > 1;
}
Поставь брейк поинты и подебаж. Посмотри в чем проблема, в инпуте или в isGrounded, в какой момент он становится true, в какой false и почему.