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
1
Question by Turkeybag · Sep 17, 2013 at 06:21 AM · functionfunction call

Function being called twice when it shouldn't be

Alright so I've got a jump function and for some reason it can be called more then once (causing superjumps). This happens more often on hills. Problem is my code shouldn't even let that happen so I have no idea how to fix it.

In function FixedUpdate (I'm gonna change it to update when I clean up my code) I have this:

 if (!Stunned && Grounded && Input.GetButtonDown("Jump") && !IsJumping)
     {
         Jump ();
         IsJumping = true;
     }

Pretty self explanatory. The problem in my jump function is from these lines of code:

         if (!Grounded)
         {
             LeftGround = true;
         }
         if (Grounded && LeftGround)
         {
             IsJumping = false;
             //other code stopping the Jump function that's not important is here
         }

That section of code is in a for loop using yield new WaitForFixedUpdate() at the end. I wanted the function to cancel when they land and doing it this way prevented the function from cancelling instantly.

So, any ideas as to what is causing the problem? I just can't see how it can be called twice due to IsJump so I'm clueless.

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
1
Best Answer

Answer by syclamoth · Sep 17, 2013 at 06:28 AM

Input.Whatever does not get reset until the next 'Update' call! This means that if you are experiencing low frame rates, you can have two calls to 'FixedUpdate' that both receive the same buttonpress. As you suspected, moving this stuff to 'Update' is itself a fix.

Comment
Add comment · Show 3 · 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 Turkeybag · Sep 17, 2013 at 06:59 AM 0
Share

Ohh I see, thanks for the help man!

avatar image Bunny83 · Sep 17, 2013 at 07:03 AM 0
Share

in short: Never process one-time events in FixedUpdate. You might miss it or process it multiple times.

avatar image syclamoth · Sep 17, 2013 at 07:20 AM 0
Share

Also, be very careful when designing systems that must interact between the Update loop and the FixedUpdate loop! You can never be sure which of those loops will be updating more often, and if your systems are too closely coupled you can end up invalidating the state of one or the other.

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

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

No proper Update and function call between objects ? 1 Answer

first function dismiss 0 Answers

Shooting and Flash 2 Answers

How to make that when someone presses a button the function of the button happens to everyone. Photon Pun 1 Answer

How to find weather the function execution completed or not 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