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
2
Question by JonK · Aug 08, 2013 at 08:58 PM · inputupdatejumpingfixedupdategetbuttondown

Jumping randomly doesn't work

My problem is that when pressing the jump button, randomly the character won't jump but it will play the jump animation.

I figure this is because I take in the input using Update() and then change a variable in another script using FixedUpdate(), but I could be wrong.

Input scirpt example:

 void Update()
 {
     motor.Jump = Input.GetButtonDown("Jump");
 }

Motor script example:

 void FixedUpdate()
    {
         if (canJump && jump && lastButtonDownTime < 0) 
         {
             lastButtonDownTime = Time.time;
         }

         if(isGrounded)
         {    
             if (!jump || !canJump) 
             {
                 lastButtonDownTime = -100;
             }
             
              //Jump only if the jump button was pressed down in the last 0.25 seconds.
             if (canJump && jump && (Time.time - lastButtonDownTime < 0.25))
             {
                 lastButtonDownTime = -100;
                 rigidbody.AddForce(new Vector3(0, CalculateJumpVerticalSpeed(), 0), ForceMode.VelocityChange);
                 isGrounded = false;
             }
         }
    }

Not sure why this is happening, any suggestions?

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 CodeAssembler · Aug 08, 2013 at 09:08 PM 0
Share

At least I can tell you that if you really want to make sure the button was actually pressed you should use Input.GetButtonUp("Jump") ins$$anonymous$$d of Down... hope that helps a bit.

avatar image JonK · Aug 08, 2013 at 09:13 PM 0
Share

Not a bad a idea, thanks.

avatar image JuanseCoello · Aug 08, 2013 at 09:32 PM 0
Share

Can you be more specific? What are you trying to do with each part of the code?

2 Replies

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

Answer by JonK · Aug 09, 2013 at 04:18 AM

I found a solution.

The problem was that Update() and FixedUpdate() are not synchronized so occasionally the input script would set jump to true and then back to false between FixedUpdate() calls. So FixedUpdate() would only see jump == false and not perform a jump.

The solution was to only set jump to true in the input script and set it to false in the motor script. This way even if jump becomes true between FixedUpdate() calls it will still be true for the next call.

Input scirpt example:

 void Update()
 {
     if(Input.GetButtonDown("Jump"))
         motor.Jump = true;
 }

Motor script example:

 void FixedUpdate()
    {
        if (canJump && jump && lastButtonDownTime < 0) 
        {
          lastButtonDownTime = Time.time;
        }
  
         if(isGrounded)
        { 
          if (!jump || !canJump) 
          {
           lastButtonDownTime = -100;
          }
  
           //Jump only if the jump button was pressed down in the last 0.3 seconds.
           if (canJump && jump && (Time.time - lastButtonDownTime < 0.3))
          {
            lastButtonDownTime = -100;
            rigidbody.AddForce(new Vector3(0, CalculateJumpVerticalSpeed(), 0), ForceMode.VelocityChange);
            isGrounded = false;
            jump = false;
          }
        }
    }

 void OnCollisionStay(Collision col)
 {
   for(int i = 0; i < col.contacts.Length; i++)
   {
             //if contact has been made on the bottom of the character
             if(col.contacts[i].point.y < (transform.position.y - topAndBottomOffset))
             {
                 //on the ground
                 isGrounded = true;
                 jump = false;
             }
     }
 
 }

This seems to be working perfectly now.

Comment
Add comment · Show 1 · 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 Disolution · Aug 05, 2014 at 12:48 AM 0
Share

Could u post the full script please? ive been having this issue as well and i dotn have enought knowledge to start messign around with teh character motor script being that i was able to get a C# (not js for personal reasons) but it is way to complex for my current level and i may end up blowing something

avatar image
0

Answer by IgorAherne · Aug 08, 2013 at 09:49 PM

I had a similar problem once. Turned out it was in 3d modeling software.

I had to link the animated object to a dummy object which wasn't moved.

As soon as your animation states the position in 3d modeling software, the character will be playing animation, but will get stuck in place. My problem was solved when I linked it to the stationary object and then exported it (with animation) to unity.

Comment
Add comment · 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

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

18 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 avatar image

Related Questions

Is Input detection ok to do in fixedupdate? 7 Answers

Eliminating input loss 1 Answer

My character won't stop jumping!!! 1 Answer

Input.GetKey missing keyboard state on Update and FixedUpdate 2 Answers

Interpolation with jumping - Acting Weird. 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