Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Robi222 · Jan 04, 2017 at 01:09 PM · c#physicscharacterjump

My 3D character is flying up when I jump

Hello!

When I jump with my 3D character and I press several times space (jump), then my character fly up in the air. What's the problem ? :/ On the object have mesh collider.

My jump script in FixedUpdate:

 if (grounded == true && Input.GetAxis("Jump") > 0)
         {
                 grounded = false;
                 rb.AddForce(new Vector3(0, jumpHeight, 0));
                 print("JUMP");
                 actions.Jump();
         }
 
         //check if we are grounded   
         groundCollisions = Physics.OverlapSphere(groundCheck.position, groundCheckRadius, groundLayer);
         if (groundCollisions.Length > 0) grounded = true;
         else grounded = false;
Comment
Add comment · Show 1
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 Cherno · Jan 04, 2017 at 02:17 PM 1
Share

You need to implement a kind of cooldown for the jumping. See, even when the character is pushed upwards after pressing space there will still be a moment where he is considered grounded so the user can still press space several more times before the overlap sphere registers no ground collisions anymore. This causes more and more force to get added to the rigidbody. You can also experiment with the other forcemodes, such as impulse.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Creeper_Math · Jan 04, 2017 at 02:42 PM

As @Cherno says, you need to add a cooldown...

Add a float to the variables to the top, as float timeCounter; and add the following script to the top of the FixedUpdate or Update (whatever you're using)

 timeCounter += -1 * Time.deltaTime;

Then replace your rb.AddForce(new Vector3(0, jumpHeight, 0)); with the following script

 if (timeCounter < 0) {
       timeCounter = 0.5f; // Or whatever delay you want
       rb.AddForce(new Vector3(0,jumpHeight,0));
       actions.Jump();
       print("Jump");
 }
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 Robi222 · Jan 04, 2017 at 02:49 PM 0
Share

Thank you so very much, it's working, but not correctly. Because the character is jumping when not walking or running, but I start walking, then character doesn't jump.

This is my script for walking/running and jumping. This is under the FixedUpdate.

 timeCounter += -1 * Time.deltaTime;
 
         if (grounded == true && Input.GetAxis("Jump") > 0)
         {
             if (timeCounter < 0)
             {
                 timeCounter = 0.5f; // Or whatever delay you want
                 rb.AddForce(new Vector3(0, jumpHeight, 0));
                 actions.Jump();
                 print("Jump");
             }
         }
 
         //check if we are grounded   
         groundCollisions = Physics.OverlapSphere(groundCheck.position, groundCheckRadius, groundLayer);
         if (groundCollisions.Length > 0) grounded = true;
         else grounded = false;
 
         if (Input.GetAxis("Horizontal") != 0)
         {
             move = Input.GetAxis("Horizontal");
 
             if (isRunning == true)
             {
                 runSpeed = 100;
                 actions.Run();
             }
             else
             {
                 runSpeed = 1;
                 actions.Walk();
             }
         }
         else
         {
             runSpeed = 0;
             actions.Stay();
         }
 
         rb.velocity = new Vector3(move * runSpeed, rb.velocity.y, 0);
avatar image Creeper_Math Robi222 · Jan 04, 2017 at 03:02 PM 1
Share

Try putting a bunch of debug logs around, ex. putting Debug.log("Checked for Grounding"); right before you check for grounding, and Debug.log("Finished Script"); at the end. If the last one, which should be placed at the very very end, doesn't show up in the debug box, then the script is never getting to or executing the move function, which is a problem

avatar image Robi222 Creeper_Math · Jan 04, 2017 at 03:08 PM 0
Share

Thanks the answer, I put debug logs, but script is executed.

Script look like this:

         timeCounter += -1 * Time.deltaTime;
 
         if (grounded == true && Input.GetAxis("Jump") > 0)
         {
            
             if (timeCounter < 0)
             {
                 timeCounter = 0.5f; // Or whatever delay you want
                 rb.AddForce(new Vector3(0, jumpHeight, 0));
                 actions.Jump();
                 print("Jump");
                 Debug.Log("Checked for Grounding");
             }
         }
 
         
         //check if we are grounded   
         groundCollisions = Physics.OverlapSphere(groundCheck.position, groundCheckRadius, groundLayer);
         if (groundCollisions.Length > 0)
         {
             grounded = true;   
         }
         else
         {
             grounded = false;
         } 
 
         if (Input.GetAxis("Horizontal") != 0)
         {
             move = Input.GetAxis("Horizontal");
             
             if (isRunning == true)
             {
                 runSpeed = 100;
                 actions.Run();
             }
             else
             {
                 runSpeed = 1;
                 actions.Walk();
             }
         }
         else
         {
             runSpeed = 0;
             actions.Stay();
         }
 
         rb.velocity = new Vector3(move * runSpeed, rb.velocity.y, 0);
         Debug.Log("Finished Script");
Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody looses momentum with contact to ground. Why? 3 Answers

Problem With a Multi-Jump C# script, robot 2d character controller 1 Answer

How to jump to a point using CharacterController? 0 Answers

How to use 3D colliders and rigidbody on 2D character controller 0 Answers

Applying Force to the Character 0 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