using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;
public class Player : MonoBehaviour
{
public float speed;
private float VELOCITY;
public float lineLength;
private float gravity = 9.8f;
public float gravitymultipleyer;
public float jumpPower;
public float cayoteTimeCounter;
private float cayoteTime;
public float jumpBufferTime;
private float jumpBufferCounter;
[SerializeField]
private LayerMask platformLayerMask;
[SerializeField]
private float maxVelocity;
// Start is called before the first frame update
void Start()
{
}
private bool isGrounded()
{
RaycastHit2D raycastHit= Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), Vector2.down, lineLength,platformLayerMask);
Color rayColor;
if(raycastHit.collider != null)
{
rayColor = Color.green;
}
else
{
rayColor= Color.red;
}
Debug.DrawRay(new Vector2(transform.position.x, transform.position.y), Vector2.down * lineLength);
return raycastHit.collider!= null;
}
// Update is called once per frame
void Update()
{
if(isGrounded())
{
cayoteTime = cayoteTimeCounter;
}
else
{
cayoteTime -= Time.deltaTime;
}
if(!isGrounded())
{
VELOCITY += gravity * gravitymultipleyer * Time.deltaTime;
Debug.Log(VELOCITY);
}
else
{
VELOCITY = 0;
}
if (VELOCITY > maxVelocity){
VELOCITY = maxVelocity;
}
float Xdirection = Input.GetAxisRaw("Horizontal");
transform.position = new Vector2(transform.position.x + Xdirection * speed * Time.deltaTime, transform.position.y);
if (jumpBufferCounter>0 && cayoteTime>0)
{
VELOCITY = -jumpPower;
jumpBufferCounter = 0;
}
if (Input.GetKeyUp(KeyCode.Space))
{
cayoteTime = 0;
}
transform.position = new Vector2(transform.position.x, transform.position.y - VELOCITY * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space))
{
jumpBufferCounter = jumpBufferTime;
}
else
{
jumpBufferCounter -= Time.deltaTime;
}
}
}