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 t-pedrob · Feb 13, 2014 at 03:29 PM · physicscharacterjumpheightphysics 2d

How to jump always with the same height

Hello guys, i have a problem. I'm using physics 2D, and i'm trying to make a character jump always the same height with Addforce, but in vain.

I alreaddy have tryed this:

 acceleration = (rigidbody2D.velocity - lastVelocity) / Time.fixedDeltaTime;
 lastVelocity = rigidbody2D.velocity;
 force = rigidbody2D.mass * Mathf.Abs(acceleration.y);
 rigidbody2D.AddForce(Vector2.up*force);

and have already tryed make velocity zero when clicked before addforce but nothing:

 rigidbody2D.velocity = new Vector2(0,0);


Can sound easy but is tricky. Any ideas?

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
0
Best Answer

Answer by nesis · Feb 13, 2014 at 04:35 PM

You can set rigidbody2D.velocity's y component to some value that'll reach the height you want. You just need to calculate what that y value needs to be in order to counter-act gravity for long enough that the apex of the jump occurs at the height you want.

Luckily, Unity's CharacterController package comes with an example of how to do this in Javascript, which I've appropriated to 2D for you:

 function CalculateJumpVerticalSpeed (targetJumpHeight : float)
 {
     // From the jump height and gravity we deduce the upwards speed 
     // for the character to reach at the apex.
     return Mathf.Sqrt(2 * targetJumpHeight * Physics2D.gravity.y);
 }

And in C#, it'd be:

 public static float CalculateJumpVerticalSpeed(float targetJumpHeight)
 {
     // From the jump height and gravity we deduce the upwards speed 
     // for the character to reach at the apex.
     return Mathf.Sqrt(2f * targetJumpHeight * Physics2D.gravity.y);
 }

Then, to set the velocity of your Rigidbody2D, you'd use this in Javascript:

 var velocity : Vector2 = rigidbody2D.velocity;
 velocity.y = CalculateJumpVerticalSpeed(myJumpHeight);
 rigidbody2D.velocity = velocity;

or in C#, it'd be:

 Vector2 velocity = rigidbody2D.velocity;
 velocity.y = CalculateJumpVerticalSpeed(myJumpHeight);
 rigidbody2D.velocity = velocity;
Comment
Add comment · Show 4 · 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 t-pedrob · Feb 13, 2014 at 05:42 PM 0
Share

Hi! Thanks, but that doesnt work for me, my rigidbody doesnt have always the same height, so with your solution is harder to jump when the character is bellow his start position and easialy when he is abobe his initial position.

Any ideas?

avatar image nesis · Feb 14, 2014 at 01:11 AM 0
Share

If you're changing the height of your rigidbody, then take that into account by getting the height of the collider and subtracting post of it from the jump height.

avatar image t-pedrob · Feb 14, 2014 at 10:25 PM 0
Share

Can you give me a example please?

avatar image DESTRUKTORR · May 25, 2015 at 04:06 PM 0
Share

Rather than giving you a code example, I'll ask a simple question:

If you want the top of your head to reach the bottom of a pole that stands directly above you, and it is 4 units above the ground, and you are 2 units tall, how high must you jump to reach that pole with the top of your head?

The answer should be at LEAST 2 units, correct?

For the sake of simplicity, let's assume we want to reach it EXACTLY, and not overshoot the jump.

If the pole is 4 units off the ground, and the top of our head is 2 units off the ground (because we are 2 units tall), then the distance between the two positions is 2 units because 4 - 2 = 2. If the pole were 5 units off the ground, and our height were still 2 units, then we would need to jump 3 units off the ground, since the pole would be 3 units above our head.

It's a simple 1-dimensional vector problem: Subtract the height off the ground of the target height from the height off the ground of the original height to get the difference between the two, or target - original = difference

What nesis stated is that if your height is changing, simply keep track of that height in a variable within the class and find the difference between the target height you want to reach with the jump and the current height of the object jumping.

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

19 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

Related Questions

How can I create a "round arc" physics based jump? 2 Answers

First person skydiver help 1 Answer

How do I make the player jump at a fixed height? 2 Answers

Rigid Character always falls (Physics) (No Root Motion) 1 Answer

Help with Ragdoll Character Controller 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