Search Unity

Is it okay to do Input in LateUpdate?

Discussion in 'Scripting' started by LeftyTwoGuns, Jul 27, 2017.

  1. LeftyTwoGuns

    LeftyTwoGuns

    Joined:
    Jan 3, 2013
    Posts:
    260
    In my shooter project, after much testing, I've discovered the best way to instantiate my projectiles is in LateUpdate, so that they match the movement and velocity of the player character. If they're instantiated in Update, the projectiles will lag behind the movement of the player

    However, doing the shooting input in Update and then instantiating in LateUpdate limits the variety of Inputs I can use, like GetButton and GetButtonDown because I'd need to use booleans to execute LateUpdate. Hence GetButtonDown functions the same as GetButton

    I tried it in the project already and I haven't noticed any issues with input accuracy/responsiveness. Are there any unforeseen issues with doing Input in LateUpdate?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    The input changes between each Update call, so reading it in LateUpdate should be completely fine, since LateUpdate runs once after Update.

    It's often recommended to read input in Update as opposed to in FixedUpdate, since FixedUpdate runs from 0 to many times between two Updates, so it's easy to miss or get to many GetButtonDown calls. For LateUpdate, though, you should be fine.
     
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I think you are good. Update and LateUpdate are tied together, so you will never add or drop calls.

    If you need finer control, its not uncommon to add your own updates in the middle. EarlyUpdate, JustBeforeUpdate, Update, AfterUpdate, LateUpdate, LaterUpdate and IPromiseThisReallyIsTheLastUpdate.

    From memory I think lordofduct's Space Puppy framework has custom updates if you need an example.
     
    silfblood and KittyAnn like this.
  4. KittyAnn

    KittyAnn

    Joined:
    Dec 25, 2018
    Posts:
    10
    :D This was VERY helpful! I spit my coffee all over the floor when I read "IPromiseThisReallyIsTheLastUpdate". LOL That was GREAT!!!
     
    Kiwasi likes this.