I have a problem where the player will dip into the ground after a jump and I don’t know how to fix this. The game is in 2D and I am using a Rigidbody2D. Here’s my code:
private void Update()
{
if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
{
nowJump = true;
}
}
// Update is called once per frame
void FixedUpdate()
{
//----------MOVEMENT----------
// Walking
float movement = Input.GetAxisRaw("Horizontal");
rb2d.velocity = new Vector2(movement * speed, rb2d.velocity.y);
// Jumping
if(nowJump == true)
{
rb2d.AddForce(transform.up * jumpForce);
nowJump = false;
}
}
private bool IsGrounded()
{
RaycastHit2D ray2d = Physics2D.BoxCast(box2d.bounds.center, box2d.bounds.size, 0f, Vector2.down, .1f, ground);
return ray2d.collider != null;
}
Any help would be greatly appreciated, thanks!