using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    //Varibles
    public float jumpHeight = 20f;
    public Transform feet;
    public LayerMask groundLayers;
    public float speed;
    public Rigidbody2D rb;

    float mx;
    bool isGrounded;

    // Update is called once per frame
    private void Update()
    {
        mx = Input.GetAxisRaw("Horizontal");

        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            Jump();
        }

    }

    private void FixedUpdate()
    {
        Vector2 movement = new Vector2(mx * speed, rb.velocity.y);

        rb.velocity = movement;
    }

    void Jump()
    {
          movement = new Vector2(rb.velocity.x, jumpHeight);

        rb.velocity = movement;
    }

    public bool IsGrounded()
    {
        Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);

        if (groundCheck != null)
        {
            return true;
        }

        return false;
    }

}