Motion for developers

Sequence of FixedUpdates

When developing custom behavior models for drives, understanding the sequence of FixedUpdates is crucial for ensuring correct behavior and performance.

  • Physics Update Cycle: All drive behavior calculations and setting of new drive positions occur within Unity's physics update cycle. By default, this cycle is set to 20ms, but it can be adjusted in the Unity settings based on performance and calculation requirements.

  • FixedUpdate Sequence: Unity executes FixedUpdate methods for all scripts within each physics cycle. However, the execution order of these FixedUpdates is not guaranteed. This means FixedUpdate in one script (Script1) could be called before or after FixedUpdate in another script (Script2).

  • Dependency Management: For behavior models that depend on other components—such as Gears or CAMs—it is essential that dependencies are calculated in a specific order. For instance, the master Drive should be calculated before its dependent slave Drive. Moreover, behavior models of the master Drive need to be processed before the master Drive's position is updated.

To ensure the correct sequence each Drive is implementing the following behavior:

  1. Drive Behavior Execution: Each Drive calls CalcFixedUpdateof all attached drive behaviors that implement theIDriveBehavior interface.

  2. Behavior Model Calculation: The attached behavior model may modify public properties of the Drive to control its status or directly set the CurrentPosition of the Drive.

  3. Position Update: The Drive then updates its position to the CurrentPosition.

  4. Subdrive Calculations: All subdrives of the Drive will have called their CalcFixedUpdate (see below).

Subdrives

Some Drives function as subdrives of a master Drive, such as in the case of Gears or CAMs.

To ensure proper behavior, these drives need to implement the IDriveBehavior interface. Additionally, during the Start method, the subdrive must call MasterDrive.AddSubDrive(thisDrive). This setup ensures that the subdrive does not run its own FixedUpdate but instead waits for the master Drive to invoke CalcFixedUpdate like explained above.

Setting a Drive Position

As previously discussed, within the Drive and Drive Behavior Execution cycle, a Drive Behavior can set the public property CurrentPositio. Due to the described sequence, this approach ensures that the position is updated correctly.

There is also a public method SetPosition(float position) available on Drives. When this method is called, the Drive will set its position once again, even if the sequence described above for that Drive has already been completed in the same FixedUpdate cycle.

Important Note: Calling SetPosition directly is not the recommended approach. It is better to implement your logic by using the IDriveBehavior interface within your behavior model attached to the Drive. This approach ensures that the Drive position updates occur in a controlled and predictable manner, adhering to the established sequence of FixedUpdates.

Drive Events

The Drive has two key events that you can utilize to hook into its update sequence:

  • OnBeforeDriveCalculation: This event is triggered before the Drive begins calculating its behavior models and updating its position. It's an ideal point to inject custom logic or make adjustments before the Drive's primary calculations occur.

  • OnAfterDriveCalculation: This event is called after the Drive has completed all behavior model calculations and updated its position, but before any SubDrives are calculated. This allows you to make further modifications or perform actions right after the main Drive update but still within the overall update sequence.

By using these events, you can effectively integrate custom behavior or adjustments into the Drive's update process.

Last updated