2
\$\begingroup\$

Im using Unity engine to create a 2D platformer. My character can single/double jump, this works fine everywhere except when i am in range of a collider with a IsTrigger set to TRUE, then my character can triple jump if jump is pressed repeatedly. This can used as an exploit in my game when collecting pickups :( How can i stop this from happening? Please assist

void FixedUpdate()
    {
        // This is the Jump code
        grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
        anim.SetBool ("Ground", grounded);
        anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);

        if (grounded)
        {
            doubleJump = false;
        }

void Update()
    { //This is pressed to get the player to jump
        if((grounded || !doubleJump) && (Input.GetKeyDown (KeyCode.Space) || didPlayerJump || wasEnemyStomped == true))
        {
            JumpAction(); // rigidbody2D.AddForce(new Vector2(0,jumpForce)) triggered in this function
            if(!grounded && !doubleJump)
            {
                doubleJump = true;
            }
\$\endgroup\$
  • 1
    \$\begingroup\$ Show some code? \$\endgroup\$ – McAden Dec 23 '14 at 0:08
  • \$\begingroup\$ I am actually experiencing a similar phenomenon, when using add force with a isGrounded bool. Will see if I can't work something out and post and update \$\endgroup\$ – Joe Dec 23 '14 at 13:06
  • \$\begingroup\$ Im new to the forum, apologies for the messy code above. I have been pulling what little hair i have left out on this issue ive done so many experiments and still the solution eludes me. Any help would be greatly appreciated. Thanks in advance \$\endgroup\$ – JDolleyArt Dec 23 '14 at 19:44
  • 1
    \$\begingroup\$ Welcome to the site. I'd recommend reading here: stackoverflow.com/help/how-to-ask and editing your original post to include the code you put in your comment. \$\endgroup\$ – McAden Dec 23 '14 at 20:30
  • 1
    \$\begingroup\$ Is the trigger for the item collection resetting grounded? Alternatively, is the item collection instance on the same layer as the ground? (as that also seems to reset 'grounded') \$\endgroup\$ – McAden Dec 27 '14 at 2:21
1
\$\begingroup\$

I had this same problem for a while. I was able to actually jump forever when I attacked. It turns out the problem was with the "whatIsGround" layer. Make sure everything is layered and the things you don't want to restart the jumpprocess are not checked in that mask layer. It solved my problem and might solve yours.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

I dont know what you are quite trying to do when the collider with a IsTrigger is true. But if you want to limit the character's jumps to a number, instead of using boolean, why not use an int to represent the number of jumps the character has left?

For example: On jump:

if (jumpsLeft >0){
    jumpsLeft -= 1;
    jump();
}

On hitting ground again:

jumpsLeft = 2;
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Hi @user55564 Thanks for the reply. I found the solution to my issue please see my comment above. I have tried your solution before however the issue was that the number of jumps left was being reset when collecting anything within the level, as my reset Jumps Left int was being reset when grounded(IsGround = true). \$\endgroup\$ – JDolleyArt Dec 27 '14 at 7:56
0
\$\begingroup\$

The code looks sound but the effect you're describing could indicate that the item collection is on the same layer as the ground - thus calling that same code and resetting grounded and doubleJump on contact. I'd put them on their own Collectible layer.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.