• 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 RadicalM1nd · Jul 10, 2014 at 10:19 PM · velocitydouble jump

2D Double Jump velocity affected by first jump

Hey guys, I downloaded the Sample Assets beta, which has a jump script that looks like this:

   // If the player should jump...
         if (grounded && jump) {
                         // Add a vertical force to the player.
                         anim.SetBool ("Ground", false);
                         rigidbody2D.AddForce (new Vector2 (0f, jumpForce));
                 } 
             } 

And I personally added the following to add a double jump:

 else if (grounded == false && jump && doubleJumpCount == 1) {
             rigidbody2D.AddForce (new Vector2 (0f, doubleJumpForce));
             doubleJumpCount = 0;
                 }
         if (grounded) {
                         doubleJumpCount = 1;
                 }
     }
 

Now first off, I'm kind of a newbie so I'm fairly proud this worked out in general. But is this clean code or is there a better way to do this?

Now, to my actual question: Currently the second jump is affected by the first jump's velocity. So if I double jump really quick in succession, the character goes a lot higher than when i jump, wait a bit, and jump again.

How can I change this so when the double jump button is pressed, the character basically loses all the momentum he has and the double jump always has the same height? I tried adding

                     rigidbody2D.AddForce (new Vector2 (0f));

Into the double jump function, but got an error.

Thanks in advance!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by rutter · Jul 10, 2014 at 10:27 PM

That looks mostly fine, aside from the problem you mentioned.

One important thing to understand about vectors, such as velocity: they have one component per axis (in your case, X and Y), and each axis is fully independent.

For example, if you want to assign only the player's vertical speed:

 rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 20f);

Or, if you want to reset horizontal speed, too:

 //the following are equivalent
 rigidbody2D.velocity = new Vector2(0f, 20f);
 rigidbody2D.velocity = Vector2.up * 20f;

If you want, you could limit the player's double jump timing a little bit; maybe they can only jump at the "peak" of their first jump? To do that, we need to understand a little bit more about velocity.

The y-component of velocity describes vertical motion. If it's positive, the character is moving up; if it's negative, the character is moving down. And if it's zero, or near zero? The character isn't moving up or down.

So, we can tell if someone's near the peak of their jump:

  • If they jumped (ie: they're not grounded)

  • Their velocity's y-component is near zero (at the peak of a jump, they "hold still" briefly)

In code, that might look like:

 float doubleJumpDifficulty = 0.2f; //max speed for double jump
 bool canJump = grounded || (doubleJumpCount > 0 && Mathf.Abs(rigidbody2D.velocity.y);
 bool jump = ??? //(this looks like your key press check?)
 if (canJump && jump) {
     rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 6f);
 }

If you want to be slightly more forgiving, you could allow a second jump any time the player is falling (maybe they walked off a platform?). Any mechanic you can describe mathematically is within your reach!

Comment
Add comment · Show 1 · 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 RadicalM1nd · Jul 11, 2014 at 08:47 AM 0
Share

Thanks a lot for the thorough answer, I got exactly what i was looking for! :)

avatar image
1

Answer by screenname_taken · Jul 10, 2014 at 10:30 PM

Try to zero out the velocity with rigidbody2D.velocity=Vector2(0,0); then jump.

Comment
Add comment · Show 1 · 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 RadicalM1nd · Jul 11, 2014 at 09:05 AM 0
Share

Hey, thanks for the answer. It worked, but without the other comment I wouldn't have known why (vertical and horizontal velocity)

avatar image
0

Answer by unknownsk · Jul 18, 2020 at 02:51 PM

thans guys what you need to do is set velocity to 0 when jumping for second time:

 void jump () {
 //secondjump
 if (candublejump) {
 rigidbodyplayer.volocity = new vector2 (0f, 0f);
 }
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

[Solved] How to shoot an object in front of the player? 1 Answer

How do I accelerate particles in Shuriken? 1 Answer

How can I make a sprite object rotate to face another object using angularVelocity? 1 Answer

Difference between translate and velocity 2 Answers


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