About update, Lateupdate and FixedUpdate in unity

         MonoBehaviour.Update 更新

        When MonoBehaviour is enabled, its Update is called every frame.


        MonoBehaviour .FixedUpdate fixed update

        When MonoBehaviour is enabled, its FixedUpdate is called every frame.

        When dealing with Rigidbody , you need to use FixedUpdate instead of Update . For example : when applying a force to a rigid body, you must apply the force to the fixed frame in FixedUpdate , not the frame in Update . ( The two frame lengths are different )


        MonoBehaviour.LateUpdate is later than update When a Behaviour is enabled, its LateUpdate         is called every frame.
        LateUpdate is called after all Update function calls. This can be used to adjust the script execution order. For example: when the object moves in Update, the camera that follows the object can be implemented in LateUpdate.



        The difference between Update and FixedUpdate :

        Update is related to the frame number of the current platform, and FixedUpdate is the real time, so when dealing with physical logic, code should be placed in FixedUpdate instead of Update.

        Update is called every time a new frame is rendered, that is, the update frequency of this function is related to the performance of the device and the rendered objects (which can be considered as the number of triangles). It may be 30 fps on a good machine, and it may be less on a bad machine. This can lead to inconsistent performance of the same game on different machines, some faster and some slower. Because the execution interval of Update is different.

        The FixedUpdate is executed at a fixed time interval and is not affected by the game frame rate. Kind of like Tick. So it is best to use FixedUpdate when dealing with Rigidbody.

        PS: The time interval of FixedUpdate can be changed in the project settings, Edit->ProjectSetting->time to find Fixedtimestep. can be modified.

        

        

        The difference between Update and LateUpdate

        In the scriptures LateUpdate is explained as a sentence: LateUpdate is called after all Update function calls. This can be used to adjust the script execution order. For example: when the object moves in Update, the camera that follows the object can be implemented in LateUpdate. I read this sentence in the fog, and then I understood it after reading other people's explanations.

        LateUpdate is executed after all Updates. For example: there are 2 steps in the game, step 1 contains Update and LateUpdate, and step 2 contains Update, then when the game is executed, each frame will execute LateUpdate after the Update in the two steps is executed. Although executed in the same frame, Update will be executed first and LateUpdate will be executed later.

        Now suppose there are 2 different scripts that control an object in Update at the same time, then when one of the scripts changes the orientation, rotation or other parameters of the object, the other footstep is also changing these things, then the orientation and rotation of the object will appear. certain repetitions. If there is another object that moves and rotates with this object in Update, the following object will shake. If it is followed in LateUpdate, it will only follow the last position and rotation after all Updates are executed, thus preventing jitter.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325867438&siteId=291194637