My character wont jump really and sticks to the ground when i just try to jump. But when i walk to either side it would let met jump normally. I cant figure out why.
using UnityEditor.Experimental.GraphView;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float groundSpeed;
public float jumpHeight;
[Range(0f, 1f)]
public float groundDecay;
public bool grounded;
public Rigidbody2D body;
public BoxCollider2D groundCheck;
public LayerMask groundMask;
float xInput;
bool yInput;
void Start()
{
}
// Update is called once per frame
void Update()
{
GetInput();
MoveWithInput();
}
void FixedUpdate()
{
CheckGround();
ApplyFriction();
}
void GetInput()
{
xInput = Input.GetAxis("Horizontal");
yInput = Input.GetButtonDown("Jump");
}
void MoveWithInput()
{
if (Mathf.Abs(xInput) > 0)
{
body.linearVelocity = new Vector2(xInput * groundSpeed, body.linearVelocity.y);
}
if (yInput && grounded)
{
body.linearVelocity = new Vector2(body.linearVelocity.x, jumpHeight);
}
}
void CheckGround()
{
grounded = Physics2D.OverlapAreaAll(groundCheck.bounds.min, groundCheck.bounds.max, groundMask).Length > 0;
}
void ApplyFriction()
{
if (grounded && xInput == 0 && !yInput)
{
body.linearVelocity *= groundDecay;
}
}
}