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 Leonardo_Temperanza · Aug 21, 2014 at 12:05 PM · rigidbody2drigidbody.addforcerigidbody.velocity

Jumping problems (how to stop a force)

Hi I have another question. This is an "auto-runner" game for ios and android, where the caracter runs to the right (rigidbody2D.velocity = new Vector2 (ForwardVelocity, rigidbody2D.velocity.y);) and you have to jump to avoid obstacles and jump on enemies.

My character jumps if I hold the button, and if I hold the button and release when the character is on the air he goes fast to the ground (like in Kid Tripp, a game for ios)

To do this, I said:

 onTheGround = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
 
 if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown(0)) {
 
             buttonDown = true;
 
         }
 
         if (Input.GetKey (KeyCode.Space) || Input.GetMouseButton(0)) {
 
             button = true;
 
         }
 
         if (Input.GetKeyUp (KeyCode.Space) || Input.GetMouseButtonUp(0)) {
 
             buttonUp = true;
 
         }
 
 
 if (buttonDown == true && onTheGround == true) {
 
             rigidbody2D.AddForce (new Vector2 (0, jumpForce), ForceMode2D.Impulse);
 
             buttonDown = false;
 
         }
 
         if (button == true) {
 
 
         }
 
         if (buttonUp == true) {
 
             rigidbody2D.AddForce (new Vector2 (0, -300));
 
 
 }
 

But when I release the button, the force stays applied forever, and that is the problem because the force stays applied when I'm on the ground after the jump and slows my character down, and if I jump again the jump is less powerful than the first one because there's the "-300" force is still applied.

I tried adding the same force but positive when I'm on the ground, and that solves the first problem (the character doesn't slow down on the ground anymore) but when I jump again the second jump is still less powerful than the first one.

I also tried adding the positive force on the air but when I jump he goes up and never return.

Does anyone know the answer to this?

The character has two colliders: a circle collider over the feet and a box collider over the body (if this helps).

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Aug 21, 2014 at 12:57 PM

Try replacing all this code with this instead:

 onTheGround = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
 if ( Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown(0) ) buttonDown = true;
 else buttonDown = false;
 if ( buttonDown && onTheGround ) {
     rigidbody2D.AddForce (new Vector2 (0, jumpForce), ForceMode2D.Impulse);
 }
Comment
Add comment · Show 5 · 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 Leonardo_Temperanza · Aug 21, 2014 at 05:09 PM 0
Share

Well if I replace the code I'm not applying the force the down. I want that the ground "attracts" the player when I release the button (applying a force in the direction of the ground). It is a little difficult to axplain, so here's a video of gameplay of a game and that's the jump I need:

https://www.youtube.com/watch?v=RioD52pr$$anonymous$$Sw

In other words I don't want that I tap and he jumps, but I want that when I keep my finger pressed on the screen it does a normal jump, and if I release my finger while the character is in the air the character falls on the ground quickly (that's why I'm adding a force down)

I hope this time you'll understand. By the way thanks again for your answer!

avatar image andrew-lukasik · Aug 21, 2014 at 06:09 PM 0
Share

Ah sure. Excuse me for forgetting about this. I think this should work then:

         onTheGround = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
         if ( Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonDown(0) ) buttonDown = true;
         else buttonDown = false;
         if ( Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonUp(0) ) buttonUp = true;
         else buttonUp = false;
 
         if ( buttonDown && onTheGround ) {
             rigidbody2D.AddForce (new Vector2 (0, jumpForce), Force$$anonymous$$ode2D.Impulse);
         }
         else if (buttonUp) {
             rigidbody2D.AddForce (new Vector2 (0, -300));
         }
avatar image Leonardo_Temperanza · Aug 21, 2014 at 08:04 PM 0
Share

Thank you so much! It works. Actually it's a little tricky, sometimes he jumps, sometimes he doesn't. Am I doing something wrong? I'm putting

if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonDown(0)) {

         buttonDown = true;

     }

     else {
         
         buttonDown = false;
         
     }

     if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButton (0)) {

         button = true;

     } 


     if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonUp(0)) {

         buttonUp = true;

     }

     else {

         buttonUp = false;

     }

In Update, and

 onTheGround = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
 
         rigidbody2D.velocity = new Vector2 (ForwardVelocity, rigidbody2D.velocity.y);
 
 
         if (buttonDown && onTheGround) {
 
             rigidbody2D.AddForce (new Vector2 (0, jumpForce), Force$$anonymous$$ode2D.Impulse);
 
             buttonDown = false;
 
         }
 
         else if (buttonUp) {
 
             rigidbody2D.AddForce (new Vector2 (0, getButtonUpForceDown));
 
         }
 
         if (button == true) {
 
 
         }

In FixedUpdate. Do I have to put:

 if (buttonDown && onTheGround) {
 
             rigidbody2D.AddForce (new Vector2 (0, jumpForce), Force$$anonymous$$ode2D.Impulse);
 
             buttonDown = false;
 
         }
 
         else if (buttonUp) {
 
             rigidbody2D.AddForce (new Vector2 (0, getButtonUpForceDown));
 
         }
 
         if (button == true) {
 
 
         }


In update? Or is there something out of place? Please help the beginner!

avatar image Leonardo_Temperanza · Aug 21, 2014 at 08:38 PM 0
Share

Ok I put the controls stuff of FixedUpdate in Update and now it seems to work.

avatar image andrew-lukasik · Aug 21, 2014 at 09:28 PM 0
Share

Thats right - FixedUpdate() is a physics-dedicated callback (usually it runs x times more often that Update()) so always put there all AddForce etc. And Update() runs once/frame so it is a good enough place to check input states.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rigidbody2D.AddForce works only once? 1 Answer

Rigidbody2D.velocity is not working. 0 Answers

Rigidbody2D adding force vs modifying velocity for character jump 1 Answer

How can i initiate an object in parabolic motion without using gravity using Rigidbody2D? 0 Answers

How to move Rigidbody2D on Player a certain amount at a certain speed, for a dash move and knockback? 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