• 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
10
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 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 reberk · Mar 02, 2013 at 04:59 PM 0
Share

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

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

Why am I losing some inputs on Update function? 0 Answers

Making a character rotate diagonal? 1 Answer

Is Input detection ok to do in fixedupdate? 7 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges