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

public class FPSInput : MonoBehaviour
{
    const float gravity = -9.8f;
    [SerializeField] private float jumpHeight = 2.0f;
    public float speed = 6.0f;
    private Vector3 velocity;
    private CharacterController characterController;
    // Start is called before the first frame update
    void Start()
    {
        characterController = GetComponent<CharacterController>();

    }

    // Update is called once per frame
    void Update()
    {
        bool groundedPlayer = characterController.isGrounded;
        //The character is grounded
        if (groundedPlayer && velocity.y < 0){
            velocity.y = -1.0f;
        }

        //Changes the height position pf the player
        if (Input.GetButton("Jump") && groundedPlayer){
            velocity.y = Mathf.Sqrt(jumpHeight*-2.0f*gravity);
        }

        velocity.y += gravity*Time.deltaTime;
        velocity.x = Input.GetAxis("Horizontal")*speed;
        velocity.z = Input.GetAxis("Vertical")*speed;

        velocity = transform.TransformDirection(velocity);

        characterController.Move(velocity* Time.deltaTime);
    }
}
