PhysicsTicker - jimdroberts/FishMMO GitHub Wiki
Handles ticking the physics simulation for a specific PhysicsScene using FishNet's TimeManager events. Ensures that physics updates are synchronized with the server's simulation loop for accurate and deterministic results in FishMMO.
-
private PhysicsScene _physicsScene
The physics scene to simulate.
-
private TimeManager timeManager
Reference to the TimeManager that provides simulation events.
-
internal void InitializeOnce(PhysicsScene physicsScene, TimeManager timeManager)
Initializes the PhysicsTicker with the given physics scene and time manager. Subscribes to the OnPrePhysicsSimulation event. physicsScene: PhysicsScene - The physics scene to simulate. timeManager: TimeManager - The time manager providing simulation events.
-
void OnDestroy()
Unity OnDestroy callback. Unsubscribes from the OnPrePhysicsSimulation event and clears references.
-
void TimeManager_OnPrePhysicsSimulation(float deltaTime)
Event handler called before physics simulation. Advances the physics scene simulation by deltaTime. deltaTime: float - The time step for the simulation.
- Requires FishNet networking, a valid PhysicsScene, and a TimeManager instance.
- Attach PhysicsTicker to a GameObject in the scene to be simulated.
- Call
InitializeOnce
with the target PhysicsScene and TimeManager to begin ticking. - The component will automatically subscribe and unsubscribe from simulation events as needed.
// Example 1: Initializing PhysicsTicker for a scene
PhysicsTicker ticker = gameObject.AddComponent<PhysicsTicker>();
ticker.InitializeOnce(myPhysicsScene, myTimeManager);
// Example 2: Automatic cleanup on destroy
// When the GameObject is destroyed, PhysicsTicker will unsubscribe from events automatically.
- Always call
InitializeOnce
after creating a PhysicsTicker to ensure proper event subscription. - Use a dedicated PhysicsTicker for each PhysicsScene that requires manual simulation.
- Ensure TimeManager is valid and active for the duration of the simulation.
- Allow PhysicsTicker to clean up automatically by relying on Unity's OnDestroy lifecycle.