Player Dips Into Ground After Jump
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!
I did some tests with your code and it seems to work as expected. A couple of things you could check that I can't gather from your script: Double check the visual portion of your player. Is the collider going into the ground or is it just the sprite? Check to make sure the values you have set for your box2d variable aren't detecting the collision too late. It's possible with a distance of 0.1f that it just might not be aligned properly. I had to make up some of the IsGrounded() and this is what I used and it works without any dipping into the ground. RaycastHit2D ray2d = Physics2D.BoxCast(transform.position, new Vector2(1.0f, 1.0f), 0f, Vector2.down, .1f, ground);