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 Spritely · Aug 28, 2020 at 10:33 PM · movementunity 2djumping2d platformerdouble jump

Unity 2D: Variable Jump Height with a Double Jump

This problem has been driving me up the wall recently and I would love some advice. I am making a 2D platformer using physics based movement (I know there are better options available but I'm a beginner). I want my character to have variable jump height based off of how long they hold down the jump button. I also want them to be able to double jump in mid-air. I would further like for them to be able to jump once if they simply fall off a platform.


I have got the double jump and the variable jump height working separately, but cannot get them to work together.


     // Jumping
     bool isJumping;
     [SerializeField] float jumpForce;
     [SerializeField] float doubleJumpForce;
     [SerializeField] int maxJumps;
     [SerializeField] int jumpsLeft;
     [SerializeField] float maxFallSpeed;
     [SerializeField] bool isGrounded;
     [SerializeField] Transform feetPos;
     [SerializeField] float checkRadius;
     [SerializeField] LayerMask whatIsGround;
 
     private float jumpTimeCounter;
     [SerializeField] float jumpTime;

 void Update()
     {
         isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
 
         moveInput = Input.GetAxisRaw("Horizontal");
 
         if (Input.GetKeyDown(KeyCode.Space) && jumpsLeft > 0)
         {
             isJumping = true;
             jumpsLeft--;
             jumpTimeCounter = jumpTime;
         }
     }
 
 private void FixedUpdate()
     {
         if (isJumping == true)
         {
             if (jumpsLeft == maxJumps)
             {
                 if (jumpTimeCounter > 0)
                 {
                     jumpTimeCounter = jumpTime;
                     playerRB.velocity = new Vector2(playerRB.velocity.x, jumpForce);
                 }
             }
         }
         else if (isGrounded == true)
         {
             jumpTimeCounter = jumpTime;
             jumpsLeft = maxJumps;
         }
 
         if (Input.GetKey(KeyCode.Space) && isJumping == true)
         {
             if (jumpTimeCounter > 0)
             {
                 playerRB.velocity = new Vector2(playerRB.velocity.x, jumpForce);
                 jumpTimeCounter -= Time.fixedDeltaTime;
             }
             else
             {
                 isJumping = false;
             }
         }
 
         if (Input.GetKeyUp(KeyCode.Space))
         {
             isJumping = false;
         }
    }
  



So far this actually works for a double jump, but I really want the double jump or the 'second jump' to have a lower jumpForce than the first one. I also don't know how to implement the double jump after falling.


Comment
Add comment · Show 1
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 Spritely · Aug 29, 2020 at 01:41 AM 0
Share

I'm also having issues where sometimes my character just continues to fly upwards. It happens more often when I maximize the game in editor. Not sure what's going on.

0 Replies

· Add your reply
  • Sort: 

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

203 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

Related Questions

isGrounded is always false, even with gravity, how do you fix that? 1 Answer

Why does this piece of code work with gravity and this one dosent 2 Answers

Rigidbody2D AddForce only working with large numbers 2 Answers

How to check if object is on ground (C#)? 1 Answer

Can't control movement while jumping 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