Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Using Update for Input Sensing and FixedUpdate for Physics Movement

Discussion in 'Scripting' started by UnityGuy1988, Jul 25, 2023.

  1. UnityGuy1988

    UnityGuy1988

    Joined:
    Jan 12, 2020
    Posts:
    24
    I've seen a number of sources that state that anything physics-related should reside in the FixedUpdate method, whereas things like controller input should be placed in Update. Quite a beginner question here, but how do I access the inputs defined in Update in physics-related Methods in FixedUpdate? I have one method in Update called 'GetInputs', with a short list of variables, and a couple of methods in FixedUpdate pertaining to the movement of my character. Can anyone shed any light on the matter?
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    I believe it's okay to read Input in FixedUpdate provided you avoid 'Down' and 'Up' functions like Input.GetKeyDown or Input.GetMouseButtonDown. The value returned by those functions is only true for the current Update() frame. FixedUpdate() and Update() can often be out of sync and so FixedUpdate() will sometimes miss the 'Down' and 'Up' moments/frames.

    You can do a test that involves reading GetKeyDown in FixedUpdate and launching bullets across the screen. You'll notice that you won't be able to launch them as regularly as you can when doing the same test from Update().

    You could always create your own GetKeyDown function that works reliably from FixedUpdate().
     
    CodeSmile likes this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Otherwise you just register the button press in a variable, or you calculate the resulting motion vector but only apply it during FixedUpdate. Strictly speaking this is not required but applying changes to physics during Update will make physics non-deterministic (not 100% sure though) and it will certainly be an issue for networked games. And last but not least there comes the deltaTime issues where you either don‘t apply it and get inconsistent input results or you do but there is a framerate hiccup and a high deltaTime causes input to exceed some „safe“ values for input - this can lead to inconsistent gameplay behaviour.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    To put into code as to how you 'bridge the gap', you just work with fields local to the class itself, and use them in both Update and FixedUpdate.

    For example:
    Code (CSharp):
    1. public class Player : Monobehaviour
    2. {
    3.    private bool _willJump = false;
    4.    
    5.    private void Update()
    6.    {
    7.        if (Input.GetKeyDown(KeyCode.Space))
    8.        {
    9.            _willJump = true;
    10.        }
    11.    }
    12.    
    13.    private void FixedUpdate()
    14.    {
    15.        if (_willJump)
    16.        {
    17.            // jump
    18.            _willJump = false;
    19.        }      
    20.    }
    21. }
     
    Ryiah likes this.
  5. UnityGuy1988

    UnityGuy1988

    Joined:
    Jan 12, 2020
    Posts:
    24
    That doesn't appear to work for me. I can only access live input data from my controller if I declare and define the input within the very class that I intend to use it.
     
  6. UnityGuy1988

    UnityGuy1988

    Joined:
    Jan 12, 2020
    Posts:
    24
    Think I've found where I'm going wrong here. Having created the member variables, when defining them in Update, I was typing the data type in (i.e. float), which probably caused it to create an entirely new variable that only exists within the context of the method I put it in, leaving the member variable itself unchanged at a constant 0, as it was when it was instantiated. Thanks for all the help! ;)