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 /
avatar image
0
Question by bamtonio · Oct 24, 2016 at 07:36 AM · movementjumping

Character not jumping when moving

I'm having a problem where does not jump while running. It does jump, but not every time i hit the up arrow. When I'm standing still however, the character jumps every time i hit the up arrow key. Even when I remove the ground check, the character will jump while running about 40% of the time.

my jump method:

 public void Jump()
     {
         if (controller.IsGrounded)
         {
             // Set our new Velocity to jump to our predesignated height
             controller.Velocity = new Vector2(rigidBody.velocity.x, Mathf.Sqrt(-2.0f * JumpHeight * Physics2D.gravity.y));
         }
     }

my Update() method:

 var horizontalInput = Input.GetAxis("Horizontal");
         // player movement
 
        if (Mathf.Abs(horizontalInput) > 0.0f || moveRight || moveLeft)                 
         {         
             

             if (horizontalInput > 0.0f || moveRight == true)                        
             {
                 if(moveRight == true)
                     horizontalInput = 1;
                 if (controller.IsGrounded || wasRunningBeforeJump)
                 {
                     wasRunningBeforeJump = true;
                     controller.Move(horizontalInput);
                 }
                 else
                     controller.Move(horizontalInput * .5f);
                 facingRight = true;
             }

             if (horizontalInput < 0.0f || moveLeft == true)                         
             {
                 if(moveLeft == true)
                     horizontalInput = -1;
                 if (controller.IsGrounded || wasRunningBeforeJump)
                 {
                     wasRunningBeforeJump = true;
                     controller.Move(horizontalInput);
                 }
                 else
                     controller.Move(horizontalInput * .5f);
                 facingRight = false;
             }

             if (controller.IsGrounded)                                              
                 RunAnimation();
     }

     //idle
     if (controller.Velocity == Vector2.zero && !touchScreenMode)                                       
     {
         IdleAnimation();
         wasRunningBeforeJump = false;
     }
     if(!moveLeft && !moveRight && touchScreenMode && controller.IsGrounded)                             
     {
         rb.velocity = Vector2.zero;
         IdleAnimation();
     }

 
     // jump
     if (Input.GetKeyDown(KeyCode.UpArrow) || jumping)                                          
     {
         if (wasRunningBeforeJump) {
             jumper.Jump();
         }
         else if (!wasRunningBeforeJump)
         {
             IdleAnimation();
             jumper.Jump();
         }
     }
 

the moveLeft and moveRight are for event triggers.

Edit: character jumps properly when I hit the UI event trigger to jump. does not jump properly using up arrow

Comment
Add comment · Show 3
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 ScaniX · Oct 24, 2016 at 09:46 AM 0
Share

How thoroughly did you debug? A first step would be to put Debug.Log() calls inside of each IF to see which expression is blocking the jump.

I suspect that either runAnimation or wasRunningBeforeJump is blocking it.

You probably need to also post the code that is setting those values.

avatar image bamtonio ScaniX · Oct 24, 2016 at 10:11 AM 0
Share

I haven't really debugged all of the if statements, but i've put Debug.Log() calls inside of both if statements in the (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow) method. everytime i tried to jump with the arrow key, it would "jump" once. However, I noticed when I clicked on the UI event trigger, it would log "jump" about 18+ times.

avatar image ScaniX bamtonio · Oct 24, 2016 at 10:30 AM 0
Share

So if you did that, you should know what is blocking him to jump.

The current code (did you edit it?) shows that the jump() method is called whenever you start pressing the up arrow. The only thing preventing it from setting the velocity then is the IsGrounded.

I still don't have enough information to go further. $$anonymous$$g. you could edit your code to add different printouts to all the necessary places and post the printout in the case the jumping fails.

Also "not jumping" means what exactly? The line of code setting the velocity is not reached? The character is just not moving vertically? Probably some other code reacts on the cursor key as well and modifies the velocity again. Check for other places modifying the velocity.

0 Replies

· Add your reply
  • Sort: 

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

Movement Script for Player... 1 Answer

Physics on a Ball 1 Answer

Adding MidAir Movement to Default Third Person Character 1 Answer

How do you calculate DeltaX and DeltaZ for a character? 1 Answer

Player can jump on non ground hazards? 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