PhysicsTicker - jimdroberts/FishMMO GitHub Wiki

Description

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.


API Access

Fields

  • private PhysicsScene _physicsScene

    The physics scene to simulate.

  • private TimeManager timeManager

    Reference to the TimeManager that provides simulation events.

Methods

  • 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.


Basic Usage

Setup

  1. Requires FishNet networking, a valid PhysicsScene, and a TimeManager instance.
  2. Attach PhysicsTicker to a GameObject in the scene to be simulated.
  3. Call InitializeOnce with the target PhysicsScene and TimeManager to begin ticking.
  4. The component will automatically subscribe and unsubscribe from simulation events as needed.

Example

// 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.

Best Practices

  • 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.
⚠️ **GitHub.com Fallback** ⚠️