Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by EdenAlon · Sep 30, 2020 at 10:46 AM · 2d2d-platformerjump2d-physics

[Unity2D Platformer] - Why is my player Double Jumping?

Hey! I'm trying to implement a jump mechanic where the longer the player presses the Jump button he will jump higher (to a certain amount). It is working correctly when I keep the button pressed. however if in mid-jump I release the jump key and press it again I am able to double jump. I don't want the player to double jump, what am I doing wrong?

I think it has to do with how I am calculating the inputs from the player. It is the first time I am trying to separate the Input methods from the physics methods itself as I learned that is better to write physics methods in Fixed Update while keeping inputs in Update.

however, I can't understand why my player is about to jump again while he is not grounded. I already debugged my isGrounded bool in the inspector and indeed after leaving the ground, I am not grounded anymore. This is really weird I'm breaking my head around this. Please help me, Thanks a lot, Eden.

  private void Update()
    {
       Inputs();
       CollisionChecks();
       AnimationSetup();
    }
 
    private void FixedUpdate()
    {
       PlayerMovement();
       PlayerJump();
       HandleDash();
       WallSlide();
       WallJump();
    }
 
 private void Inputs()
    
    {
       //Horizontal Inputs
       xMoveInput = Input.GetAxisRaw("Horizontal"); //GetAxisRaw meaning snappy movement, remove raw for fluidity
       
       //Jump Inputs
       
          if (Input.GetButtonDown("Jump"))
          {
             isJumpButtonPressed = true;
          }
 
          if (Input.GetButtonUp("Jump"))
          {
             isJumpButtonPressed = false;
          }
 
 private void CollisionChecks()
    {
       isGrounded = Physics2D.OverlapBox(groundCheckPos.position, groundCheckSize, 0, groundLayer);
       isTouchingWall = Physics2D.OverlapBox(wallCheckPos.position, wallCheckSize, 0, groundLayer);
    }
 
 private void PlayerJump()
    {
       
       if (isJumpButtonPressed && isGrounded)
       {
          isGrounded = false;
          jumpTimeCounter = jumpTime;
          rb.velocity = new Vector2(rb.velocity.x, jumpForce);
          isJumpButtonPressed = false;
          
       }
 
       if (Input.GetButton("Jump") && !isGrounded)
       {
          if (jumpTimeCounter > 0)
          {
             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
             jumpTimeCounter -= Time.deltaTime;
          }
          else
          {
             isGrounded = true;
          }
          
       }
 
       if (!isJumpButtonPressed)
       {
          isGrounded = true;
       }
       
    }
    
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by spooneystone · Sep 30, 2020 at 01:42 PM

Put your CollisionChecks() in Fixedupdate() as it is checking for the ground.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by EdenAlon · Sep 30, 2020 at 04:18 PM

hmmm.. I tried what you said (spooneystone) and it didn't fix the problem, however, moving all my methods from fixed update to update, solved it. everything is so responsive and the double-jump never occurs.

So I don't get it? how should you use fixed Update? what am I doing wrong? is it really so bad for performance if I put my methods in Update for a 2d platformer?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Richardson_37 · Oct 07, 2020 at 11:32 AM

Thanks for the solution yourtexasbenefits

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by sztobar · Oct 10, 2020 at 07:47 PM

@EdenAlon at the end of PlayerJump method you have a condition:

        if (!isJumpButtonPressed)
        {
           isGrounded = true;
        }

so each time a player stops pressing jump button mid-air you reset isGrounded making it possible to jump again. Can you explain what is intention of this code block?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

305 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Velocity in Unity2D 2 Answers

What is the best/easiest way to align a 2D rigidbody to specific sections of a sprite? 1 Answer

Determining 2D Velocity in a specific direction? 1 Answer

Why is there a gap between my player and the wall during wall slide? SOLVED 2 Answers

C# 2D Jumping with AddForce doesnt work 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges