Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Seeven · Oct 01, 2015 at 08:56 PM · rigidbody2djumpaddforceforcerigidbody.addforce

Predicting max jump height of a force impulse

Hi !

I have a rigidbody2D to which I apply this jump routine :

 IEnumerator JumpRoutine()
 {
     Vector2 jumpVector = new Vector2 (0f, jumpForce);
 
     // === Velocity Cut ===
     // source : http://gamasutra.com/blogs/DanielFineberg/20150825/244650/Designing_a_Jump_in_Unity.php
 
     // Add force on the first frame of the jump
     rigidbody2D.velocity = Vector2.zero;
     rigidbody2D.AddForce(jumpVector, ForceMode2D.Impulse);
     
     // Wait while the character's y-velocity is positive (the character is going up)
     while(isPressingJump && rigidbody2D.velocity.y > 0)
     {
         yield return null;
     }
 
     // [not important] If the jumpButton is released but the character's y-velocity is still positive...
     if (rigidbody2D.velocity.y > 0)
     {
         //...set the character's y-velocity to 0;
         rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
     }
 }

Basically, it applies a force impulse of jumpForce when the user presses Jump. As long as the user keeps the Jump key down, the rigidbody2D acts like normal (going as high as the initial force impulse allows him to). But when he releases the key, the rigidbody2D brutally stops going up (y-velocity to 0).

I tried to calculate the maximum height the rigidbody2D can reach if the user keeps the Jump key down long enough. Is there a way to simulate/predict the jump and place a mark at the rigidbody's future position ? Or can it be calculated with some physics ?

(My goal is to draw a debug line showing the max height the player can reach with a jump).

Thanks! Seeven.

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

1 Reply

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

Answer by Seeven · Oct 04, 2015 at 05:10 PM

For the record, I found what I needed :

Here is how I calculate the max jump height :

 void CalculateJumpLine() 
 {
     float g = rigidbody2D.gravityScale * Physics2D.gravity.magnitude;
     float v0 = jumpForce / rigidbody2D.mass; // converts the jumpForce to an initial velocity
     float maxJump_y = groundCheck.position.y + (v0 * v0)/(2*g);
     
     // For Debug.DrawLine in FixedUpdate :
     lineStart = new Vector3(-100, maxJump_y, 0);
     lineEnd = new Vector3(100, maxJump_y, 0);
 }

Hope it helps some people.

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 TFan · May 26, 2017 at 10:41 AM 0
Share

in my way i just changed 0 to little value like 3-4 to smooth bruttally stops in air :) if (GCRB.velocity.y > 0) { GCRB.velocity = new Vector2(GCRB.velocity.x,4); }

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

30 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

Related Questions

How to AddForce up to a certain height and then stop? 0 Answers

Instantiated Prefab no Rigidbody2D on Update function 1 Answer

my 2d character isnt jumping with constant velocity on applying force 1 Answer

rigid.velocity.x = 0 for unknow reason 0 Answers

Bullet not moving rigidbody2d,AddForce not working for bullet 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