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 /
This question was closed Dec 07, 2013 at 07:37 PM by Julien-Lynge for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by BobbleHead · Dec 07, 2013 at 06:33 PM · buttonaddforceforce

AddForce adding together on 2 button presses

I've been making a platformer for some time, and the player can both jump and have an upwards force applied so the player can hover.

However, when I press jump, then immediately press gravity, the forces are added together and the player goes a lot higher than intended, and this would break the game in make instances.

How can I detect that I'm jumping so that if the player presses the gravity button moments later the two forces aren't added together?

I've copied in the code for the problem:

 function FixedUpdate () {
 
         if(grounded == true){
             if (jumpKeyPressed){
                 if(inwater == false){
                     rigidbody.velocity.y += jumpSpeed;
                     }else{
                     rigidbody.velocity.y -= (jumpSpeed-(jumpSpeed/3));
                 }
             }
         }
 
         
         if(Gravity == false){
             Physics.gravity = Vector3(0, gravity, 0);
         }
         
         if(Gravity == true){
             Physics.gravity = Vector3(0, -gravity, 0);
         }
         
         jumpKeyPressed = false;
         if (rigidbody.IsSleeping()){
             rigidbody.WakeUp();
         }
         grounded = false;
     }
 

Any ideas would be appreciated.

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

  • Sort: 
avatar image
0
Best Answer

Answer by Julien-Lynge · Dec 07, 2013 at 07:10 PM

I didn't see a question in there. I'm guessing you want to disallow the player from jumping and turning off gravity at the same time? In which case, can't you just add a check in your FixedUpdate? If jump, do x. If jump and gravity, do y, which removes the gravity effect from the jump effect.

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 BobbleHead · Dec 07, 2013 at 07:16 PM 0
Share

I added the question "How can I detect that I'm jumping so that if the player presses the gravity button moments later the two forces aren't added together?"

But i don't know how to limit the force being added; I guess that could be the route of issue.

avatar image Julien-Lynge · Dec 07, 2013 at 07:22 PM 0
Share

Oh, I think I may see - you want to know not if the player pressed jump this frame, but if the player pressed jump in a previous frame and hasn't yet come back in contact with the ground?

Isn't that what your grounded bool is for? Can you check that and disallow changing gravity while it's true?

avatar image BobbleHead · Dec 07, 2013 at 07:27 PM 0
Share

yeah; woops.

checked gravity was true then included if(grounded == true){ jumpSpeed = 0; }else{ jumpSpeed = 40; }

just about does the trick.

avatar image Julien-Lynge · Dec 07, 2013 at 07:37 PM 0
Share

Awesome! I've marked the answer as correct and closed the question.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Calculate forces required to rotate around an arbitrary point regardless of center of mass position 1 Answer

Adding Force with a GUI button 5 Answers

AddForce to object 3 Answers

AddForce to the Left? 3 Answers

How do I apply a force to the other object in a collision? 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