3
\$\begingroup\$

I know some differences between Update() and FixedUpdate() and recently I have used both functions in one script. The code is working, but I have a question:

Does using both of these functions cause any problems? Is it safe to use them both?

\$\endgroup\$
5
\$\begingroup\$

Update Order

When you’re keeping track of game logic and interactions, animations, camera positions, etc., there are a few different events you can use. The common pattern is to perform most tasks inside the Update function, but there are also other functions you can use.

FixedUpdate:

FixedUpdate is often called more frequently than Update.

It can be called multiple times per frame if the frame rate is low and it may not be called between frames at all if the frame rate is high. All physics calculations and updates occur immediately after FixedUpdate. When applying movement calculations inside FixedUpdate, you do not need to multiply your values by Time.deltaTime.

FixedUpdate is called on a reliable timer, independent of the frame rate

.

Update:

Update is called once per frame. It is the main workhorse function for frame updates.

LateUpdate:

LateUpdate is called once per frame after Update has finished.

Any calculations that are performed in Update will have completed when LateUpdate begins. A common use for LateUpdate would be a following third-person camera. If you make your character move and turn inside Update, you can perform all camera movement and rotation calculations in LateUpdate. This will ensure that the character has moved completely before the camera tracks its position.

To use Fixed Update, Update and Late Update Simultaneously is safe.

using UnityEngine;
using System.Collections;

public class CheckFunctionExecutionOrder : MonoBehaviour
{
    void FixedUpdate ()
    {
        Debug.Log("FixedUpdate time :" + Time.deltaTime);
    }
    
    
    void Update ()
    {
        Debug.Log("Update time :" + Time.deltaTime);
    }
    
    
    void LateUpdate()
    {
       Debug.Log("late Update time :" + Time.deltaTime);
    }
}

So the execution order for all of is different but they run simultaneously.FixedUpdate is updated for fixed timeframe independent of device fps, Update is called per frame depend on device fps.

enter image description here

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Excellant answer! It might be worth including that you can create a coroutine that waits for FixedUpdate before executing; starting this coroutine from a start method effectivley gives you a LateFixedUpdate (which, while not as useful, does have its uses). \$\endgroup\$ – Gnemlock Jun 21 '17 at 8:21
  • \$\begingroup\$ Multiplying by Time.deltaTime is still a good idea in FixedUpdate. It lets you express things like speeds in "units per second" instead of "units per tick" and if you change your fixed timestep you won't have to go back and re-tune all your parameters to get the same speeds. The advantage in FixedUpdate is that Time.deltaTime always returns the stable fixedDeltaTime when accessed from FixedUpdate, rather than getting a variable result every frame based on framerate and time scaling, the way it does in Update. \$\endgroup\$ – DMGregory Jun 21 '17 at 12:05
2
\$\begingroup\$

They are actually all running at the same time, your scripts just do stuff in those functions. Update is called once per frame and FixedUpdate is called once every "physics update" (which is once every fixed amount of time that you can adjust).

There's also LateUpdate, if you want certain functions to run after the Update function of that reference frame. For a full list of messages, see the MonoBehaviour reference.

Here's a link to the Unity tutorial that covers this.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Physics Update is controllable? I mean if i set Fixed TimeStep to some value then the Fixed Update will run after that time? and the game physics will also work after that amount of time ? \$\endgroup\$ – Muhammad Faizan Khan Jun 21 '19 at 11:26
  • \$\begingroup\$ @MuhammadFaizanKhan That should be how it works. I'm not sure in the current version of Unity though, it was how it was when I wrote the answer. Basically, update is updated every frame and physics has its own frame rate and update function separate from the other update function. \$\endgroup\$ – John Hamilton Jun 21 '19 at 12:13
0
\$\begingroup\$

It is safe to use Update(), FixedUpdate(), and LateUpdate() all in one script. At least I do and have never had any issues. I'll generally use Update() for my main loops, FixedUpdate() for stuff like physics, and LateUpdate() for loops I want to execute after everything else.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.