using System.
Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using Unity.Burst.CompilerServices;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public float speed;
public float jumpForce;
private bool isJumping;
private float move;
[SerializeField] private LayerMask layerGround;
Rigidbody2D rb;
Animator anim;
SpriteRenderer spR;
BoxCollider2D bxC2D;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
spR = GetComponent<SpriteRenderer>();
bxC2D = GetComponent<BoxCollider2D>();
}
// Update is called once per frame
void Update()
{
Move();
Jump();
}
void Move()
{
move = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(move * speed, rb.velocity.y);
if(move > 0)
{
anim.SetBool("Run", true);
spR.flipX = false;
}
if(move < 0)
{
anim.SetBool("Run", true);
spR.flipX = true;
}
if(move == 0)
{
anim.SetBool("Run", false);
}
}
void Jump()
{
if(Input.GetButton("Jump"))
{
if(!isJumping)
{
rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
}
}
if(rb.velocity.y > jumpForce)
{
rb.velocity = rb.velocity.normalized * jumpForce;
}
if(isGrounded())
{
isJumping = false;
}
else
{
isJumping = true;
}
}
private bool isGrounded()
{
RaycastHit2D ground = Physics2D.BoxCast(bxC2D.bounds.center,
bxC2D.bounds.size, 0, Vector2.down, 0.1f, layerGround);
return ground.collider !=null;
}
}