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 bob1234 · Aug 19, 2014 at 09:19 AM · c#jumpaddforce

how to control gravity for addforce ?

So i just wanna know how i could go about controlling the gravity for the rigidbodys addforce function ? like when i jump the player reaches the end of his jump and like floats for maybe 0.2 seconds, then drops down again, it feels very floaty and unrealistic (not that the game is supposed to be realistic, but u get me), and he also shoots up in the air whenever i double jump.

Ive tried setting the gravity in the physics2D project settings, from the default -9.81 to like -15 - -19. But it feels like its too much. The box collider on my character is 1 unit tall, and the objects in the scene are two units tall, if that helps. my question is: how do i make the jump so that it feels more realistic, so that the character falls directly to the ground after reaching the jump limit, and doesnt float for 0.2 seconds, and also doesnt shoot straigt up in the air when i doublejump, but jumps with same force as the first jump.

My movement code is:

 void FixedUpdate()
     {
 
         var absVelY = Mathf.Abs (rigidbody2D.velocity.y);
         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
         if (controller.moving.x != 0) 
         {
             transform.localScale = new Vector3 (controller.moving.x > 0? 1 : -1, 1, 1);
             animator.SetInteger ("AnimState", 1);
         } else 
         {
             animator.SetInteger ("AnimState", 0);
         }
         if (absVelY < 0.02f) 
         {
             grounded = true;
         } 
         else 
         {
             grounded = false;
         }
         if (grounded) 
         {
             jumpCount = 0;
         }
 
         if (jumpCount < maxJump)
         {
             if (Input.GetKeyDown ("space")) 
             {
                 rigidbody2D.AddForce (new Vector2 (0, jumpForce));
                 jumpCount++;
             }
         }
    }

Comment
Add comment · Show 2
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 coldjalapeno · Aug 19, 2014 at 09:27 AM 0
Share

For the double jump problem refer to this live training tutorial: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/infinite-runner

If I remember correctly you need to reset the velocity to zero upon adding the second jump.. but I might be off.

avatar image bob1234 · Aug 19, 2014 at 09:37 AM 0
Share

ah yea thats true, ill try and fiddle around with that, thanks, ill return with results

1 Reply

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

Answer by senc01a · Aug 19, 2014 at 10:25 AM

  • Jump more realistic

You can do this by increasing the amount of gravity (like you did with -19). Try -40 for instance. Keep in mind that your jumpforce needs to be significantly higher as well. Play with higher values of the jumpforce and gravity and you will see how you get a much snappier jump.

  • Double jump same as the first jump This is somewhat more tricky. You are using forces to move the rigidbody. Because of this, you need to calculate the force that it would take the counter the current vertical speed, plus the force to get the player to jump same as it did on the first jump. This is rather difficult, so a much easier way is to simply set velocities instead of adding forces.

So basically in line 31, instead of applying a force rigidbody2D.AddForce (new Vector2 (0, jumpForce)); , set the velocity similar to what you do in line 5

rigidbody2D.velocity = new Vector2(rigidbody2D.velocity,x, 5.0);

Comment
Add comment · Show 2 · 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 bob1234 · Aug 19, 2014 at 11:15 AM 0
Share

Hey thanks for your reply. Hmm it kinda works this way, it jumps with the same force each time now it seems when using velocity, so ur answer helped, but still it's kinda meh.. it's more of a value tweaking question i think, than a direct approach.. i'll vote your answer up :)

avatar image senc01a · Aug 19, 2014 at 11:42 AM 0
Share

Thanks! I'm glad that my answer helped you :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Jump Force gets added up with repeated input and causes character to fly off 0 Answers

Player sometimes lags and jumps very high when instantiating prefab 1 Answer

have 3rd person make a side jump -1 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