Search Unity

Update or FixedUpdate?

Discussion in 'Scripting' started by WinterSoldier38, May 1, 2016.

  1. WinterSoldier38

    WinterSoldier38

    Joined:
    May 1, 2016
    Posts:
    3
    Hi !

    To cut a long story short, I have a very simple code, for now, which makes the rigidbody velocity equals a new vector3 consisting of Input.GetAxes multiplied by speed.

    Should I put this code in Update multiplying movement by Time.deltaTime or in FixedUpdate?

    Should I handle the GetAxes in Update then cast it to a variable and use it in FixedUpdate ?

    Thanks in Advance
     
  2. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    If you are doing any calculations with the rigidbody, put it in the FixedUpdate. If you are just assigning values to properties you can really put that where ever...
     
    Mycroft likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Input in Update. Physics in FixedUpdate.
     
    Rakun665 and Mycroft like this.
  4. frekiidoochmanz

    frekiidoochmanz

    Joined:
    Feb 7, 2016
    Posts:
    158
    I read before that Fixed updates in general are called only once per frame. (minimum)
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No. Update is called once per frame.

    FixedUpdate can be called 0, 1, or many times per frame.
     
    Mycroft and frekiidoochmanz like this.
  6. frekiidoochmanz

    frekiidoochmanz

    Joined:
    Feb 7, 2016
    Posts:
    158
    That makes sense, I now understand it depends on what the developer set the update rate as.
     
    Mycroft likes this.
  7. WinterSoldier38

    WinterSoldier38

    Joined:
    May 1, 2016
    Posts:
    3
    Actually, that's what everyone says, but I want to know what to do in my case.

    Should I put the whole code in FixedUpdate?
    Should I make variables and assign these variables in Update to the Axes?
     
  8. WinterSoldier38

    WinterSoldier38

    Joined:
    May 1, 2016
    Posts:
    3
    Can I then put my whole game code in FixedUpdate?
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the problem with putting input code into FixedUpdate is that because fixedupdate isn't guaranteed to be called every frame your game might miss the input.

    This is hugely simplified but it'll give you the sense of the issues you might face:
    Let says you're sticking with the default 50 frames a second for the physics tick, but you're game isn't too taxing and update is running at 100 frames per second.

    Now you tap the space bar to "jump" (or whatever) such that the input only lasts for 1 frame. If you have the input code in the fixed update you've got a 50/50 chance the input code is caught on the same frame as a physics tick and processed.


    What you "should" do is highly dependant on what you are trying to do. If you're writing a fast paced fighting game, you'll notice the problems. If you're writing something a lot slower, you might not. o_O
     
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    If you're setting the velocity based on continuous input (controller stick/keyboard button/mouse held down), you can set it in Update and call it a day. That would be appropriate for something like a shmup.

    If you're reading input events, and applying force a single time, as you'd do if a single key press caused a jump, you'll want to split it up - do input in Update and apply the input in FixedUpdate.
     
    Kiwasi likes this.