Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by reberk · Mar 02, 2013 at 04:48 PM · javascriptinputupdatefixedupdate

Eliminating input loss

I've been playing around with the prepackaged CharacterMotor script and have run into some confusion about how to best relate Update and FixedUpdate to one another.

All my input functions are placed tidily inside Update, then referenced in FixedUpdate, where their physical effects are executed. The problem I run into is that with any sort of instantaneous trigger, like GetKeyDown, sometimes FixedUpdate misses it and thus nothing happens.

An example of a script that would cause this:

     Update(){
       inputSuperJumpDown = Input.GetButtonDown ("SuperJump");
     }
     
     FixedUpdate(){
       if (inputSuperJumpDown){
         Debug.Log ("I'm super jumping!");
       }
     }

How is it that the prepackaged CharacterMotor manages to keep all its default jumping/physical behaviour contained within FixedUpdate (or functions called within FixedUpdate) seemingly without any loss of input?

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

Answer by Bunny83 · Mar 02, 2013 at 04:58 PM

The way you have your script at the moment would be the same as if you had read the input directly in FixedUpdate. Input changes only between Frames. So each Update the input changes. The problem with FixedUpdate is that it might not be called between two updates. Since you set / reset your variable every Update it just directly reflects the state you get from Input.GetKeyDown.

One way to handle one time events is this:

 function Update()
 {
     if (Input.GetButtonDown ("SuperJump"))
         inputSuperJumpDown = true;
 }
  
 function FixedUpdate()
 {
     if (inputSuperJumpDown)
     {
         inputSuperJumpDown = false;
         Debug.Log ("I'm super jumping!");
     }
 }

So when the key is pressed the variable remembers the "down event". Once FixedUpdate is called it sees the state and resets it.

However for one time events you don't have to use FixedUpdate. It's only important if you apply physics over time and therefore over multiple frames. So just put your one-time-code in Update:

 function Update()
 {
     if (Input.GetButtonDown ("SuperJump"))
     {
         Debug.Log ("I'm super jumping!");
     }
 }
  
Comment
Add comment · Show 2 · 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 reberk · Mar 02, 2013 at 04:59 PM 0
Share

That is incredibly helpful and clears up a lot for me. Thanks a bunch!

avatar image Juandapp · Nov 10, 2021 at 02:03 PM 0
Share

THAAAAAAAAAAAAAAAAAAAAAAAAAAANK YOUUUUUUUUUUUUU. This fixed my life

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

11 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Jumping randomly doesn't work 2 Answers

Input.GetMouseButtonDown(0) running through my if statments too quickly 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 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