heres the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
Rigidbody2D rb;
Animator animator;
[SerializeField] Transform groundCheckCollider;
[SerializeField] LayerMask groundLayer;
[SerializeField] const float groundCheckRadius = 0.2f;
[SerializeField] public float speed;
[SerializeField] public float jumpPower;
float horizontalvalue;
bool isRunning;
bool facingRight = true;
float runSpeedModifier = 2f;
[SerializeField] bool isgrounded;
[SerializeField] bool jump;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void Update()
{
horizontalvalue = Input.GetAxisRaw("Horizontal");
if(Input.GetKeyDown(KeyCode.LeftShift))
{
isRunning = true;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
isRunning = false;
}
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;
}
else if (Input.GetKeyUp(KeyCode.Space))
{
jump = false;
}
}
void FixedUpdate()
{
GroundCheck();
Move(horizontalvalue, jump);
}
void GroundCheck()
{
isgrounded = false;
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckCollider.position, groundCheckRadius, groundLayer);
if (colliders.Length > 0)
isgrounded = true;
}
void Move(float dir,bool jumpFlag)
{
if(isgrounded && jumpFlag)
{
isgrounded = false;
jumpFlag = false;
rb.AddForce(new Vector2(0f,jumpPower));
}
#region move & run
float xVal = dir * speed * 100 * Time.fixedDeltaTime;
if (isRunning)
{
xVal *= runSpeedModifier;
}
Vector2 targetVelocity = new Vector2(xVal, rb.velocity.y);
rb.velocity = targetVelocity;
if (facingRight && dir < 0)
{
transform.localScale = new Vector3(-5, 5, 1);
facingRight = false;
}
else if (!facingRight && dir > 0)
{
transform.localScale = new Vector3(5, 5, 1);
facingRight = true;
}
animator.SetFloat("Xvelocity", Mathf.Abs(rb.velocity.x));
#endregion
}
}