There is an error with my script when performing a double jump. When performing the second jump, it will register it does the first jump (again) and the second jump which results in a total of 3 jumps instead of 2. This results occurs when I implement a coyote timer and the isGrounded check. *This is groundCheck.cs* Code (CSharp): private void OnCollisionEnter2D(Collision2D collision) { Debug.Log("On collison enter"); if (collision.collider.tag == "platform") { Player.GetComponent<movement>().isGrounded = true; animator.SetBool("IsJumping", false); Debug.Log(Time.frameCount + "This is hitting" + collision.collider.gameObject.name); } } private void OnCollisionExit2D(Collision2D collision) { Debug.Log("On collison exit"); if (collision.collider.tag == "platform") { Player.GetComponent<movement>().isGrounded = false; } } *This is movement.cs* Code (CSharp): // jump and double jump if (Input.GetButtonDown("Jump") && hangCounter > 0f && isHanging == false && doneFirstJump == false) // //ForceMode2D.Impulse = instand force rather than gradual speed // current issue is that it registers the first jump twice { rb.AddForce(new Vector2(0, JumpHeight), ForceMode2D.Impulse); inAir = true; animator.SetBool("IsJumping", true); isHanging = true; doneFirstJump = true; Debug.Log(Time.frameCount + "IS IT GROUNDED?: (FIRST JUMP)" + isGrounded); Debug.Log(Time.frameCount + "this is single jump"); } if (Input.GetButtonDown("Jump") && isGrounded == false && inAir == true) { rb.AddForce(new Vector2(0, extraJumpHeight), ForceMode2D.Impulse); //ForceMode2D.Impulse = instand force rather than gradual speed inAir = false; Debug.Log("this is the double jump"); Debug.Log(Time.frameCount + "IS IT GROUNDED?: (SECOND JUMP)" + isGrounded); } The following code results in this results: http://prntscr.com/11o9xj6 Right after the first (single jump), the isGrounded boolean is checked to true for 4 frames (this should be false) before the onCollisionExit is executed which makes isGrounded false. The 4 frame is what results in the single jump being executed with the second jump. I have also made sure there are no other colliders the ground collider can hit when in the air. I am not sure what causes this problem and how to fix it. Any help you be greatly appreciated!
correction since the image doesn't work: The following code results in this: http://prntscr.com/11o9xj6
Input.GetButtonDown() is not to be used anywhere except in Update(). See the docs for why, but it basically boils down to this timing diagram: https://docs.unity3d.com/Manual/ExecutionOrder.html That also shows you when the collider callbacks get called. Similarly, anything to do with physics (rigidbodies) should ONLY be done in FixedUpdate(). Generally gather all input from Input.GetButtonDown(); and store it to temporary boolean values. This is broadly called "collecting input." Then in other functions such as in FixedUpdate() or your collider callbacks, act upon those temporary boolean values and clear them back to false, effectively "consuming" the previously-collected input.
Thanks so much! this fixed my issue, i've changed the input key detection to be Update() and then in FixedUpdate(), I do the physics and jump stuff