- Home /
Player can jump infinitely when under collision
I'm working on a little 2d platformer game and i can't figure out why the player can continue jumping when he's under a collision with the same tag as the ground. Here is the player collision and movement code:
private int playerSpeed = 10;
private int playerJumpHeight = 1000;
private int playerDoubleJumpHeight = 800;
private float moveX;
public bool Grounded;
public bool doublejump = false;
private int jumps;
public float distancetoground = 0.9f;
void Update () {
PlayerMove();
}
void PlayerMove()
{
//Controls
moveX = Input.GetAxis("Horizontal");
if (Input.GetButtonDown("Jump") && Grounded == true && doublejump == false)
{
Jump();
jumps++;
}
else
{
if (Input.GetButtonDown("Jump") && doublejump == true)
{
DoubleJump();
jumps++;
}
}
//Animation
if (moveX != 0)
{
GetComponent<Animator>().SetBool("isRunning", true);
}
else
{
GetComponent<Animator>().SetBool("isRunning", false);
}
if (Grounded == false)
{
GetComponent<Animator>().SetBool("isJumping", true);
}
else
{
GetComponent<Animator>().SetBool("isJumping", false);
}
if (jumps == 2)
{
GetComponent<Animator>().SetBool("DoubleJump", true);
}
else
{
GetComponent<Animator>().SetBool("DoubleJump", false);
}
//Player direction
if (moveX < 0.0f)
{
GetComponent<SpriteRenderer>().flipX = true;
}
else if (moveX >0.0f)
{
GetComponent<SpriteRenderer>().flipX = false;
}
//Physics
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);
}
void Jump()
{
//Jump code
doublejump = true;
Grounded = false;
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(gameObject.GetComponent<Rigidbody2D>().velocity.x, 0);
GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpHeight);
}
void DoubleJump ()
{
//Double jump code
Grounded = false;
doublejump = false;
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(gameObject.GetComponent<Rigidbody2D>().velocity.x, 0);
GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerDoubleJumpHeight);
}
private void OnCollisionEnter2D(Collision2D col)
{
//Ground check
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down);
if (hit.collider != null && hit.distance < distancetoground && hit.transform.tag != "Ground")
{
Grounded = true;
jumps = 0;
doublejump = false;
}
}
}
And here is what the infinite jumping looks like: https://gyazo.com/286d2b6d3a5ea9e246acee0fca440952 In both cases i mashed the spacebar after jumping. I should also include that both the ground and the blocks have the same "Ground" tag because they're part of the same tilemap.
Assu$$anonymous$$g the hitbox is convex you could try to use collision2d.GetContact(collision2d.contactCount - 1).normal
. This should return the normal vector for the last contact in the collision. If the normal vector points in the general direction upwards, you can be quite certain your character is touching something beneath it and it should be safe to reset the number of jumps. Beware that this is not the case if the model of your character is not convex.
Answer by LCStark · Sep 30, 2018 at 11:00 AM
When your character collider hits a collider tagged "Ground", you reset your jumping variables. It doesn't matter whether you hit the ground or the ceiling - to the collider all that matters is that you're hitting a "Ground" collider.
You can solve this by having two colliders on your character - one around its feet to detect when it's on the ground, and one for the rest of the body to collide with walls and ceilings.
EDIT
Have you tried to Debug.Log
the values your Raycast
returns? Depending on your setting, your raycast may be hitting the platform above your head or return distance small enough to pass your check.
And how would i call the 2 different colliders in the script?
Attach your colliders to empty child gameobject: screen. You can then either check them by their name (collider.gameObject.name), or give them different tags.
Answer by Captain_Pineapple · Sep 30, 2018 at 11:02 AM
Hey there,
i'd recommend changing your OnCollisionEnter function completly. This function already provides you with all the information that should be needed. There is no need for a raycast.
So perhaps you should try something along the lines of this example:
private void OnCollisionEnter2D(Collision2D col)
{
//Check if whatever we hit was something tagged ground and if the contact point is above our character.
//this assumes that your character transform is somewhere in the middle or lower part of your sprite.
if (col.transform.CompareTag("Ground") && col.contacts[0].point.y < transform.position.y)
{
Grounded = true;
jumps = 0;
doublejump = false;
}
}
hope this points you in the right direction. No guarantuee for instant working of this, I did not specifically test this code. I also assume that your up direction is the y Axis.
I've tried this and when i jump at a wall i get stuck in the falling animation that plays after the double jump if i stick to the wall
well in that case you could also include another statement to check how far the x-distance between you character and the point you hit is. Like that you should be able to detect if you hit a wall or the ground.