Photon Unity Networking 2 (PUN2)
Multiplayer implementation for NWH Vehicle Physics 2 using Photon Unity Networking 2. Works for both dedicated server and host+client scenarios.
Prerequisites
- Vehicle and scene must be set up for single-player first (see Quick Start)
- PUN2 must be imported and configured (verify with Photon's demo scenes first)
Setup
Import Sample
Import the Multiplayer PUN2 Sample via Package Manager:
- Open Package Manager
- Select NWH Vehicle Physics 2
- Expand Samples section
- Import Multiplayer PUN2 Sample
Scene Setup
Create a PhotonManager GameObject with:
OnJoinedInstantiatecomponentConnectAndJoinRandomcomponent- Set position offset >10
- Enable Auto Connect
Vehicle Setup
Add PhotonMultiplayerVehicle component to your vehicle. This automatically adds required components:
- PhotonView
- PhotonRigidbodyView
- PhotonTransformView
Important: PhotonView's Observe option must NOT be set to "Off".
The component automatically configures ObservedComponents on initialization. Configurable properties:
- Send Rate: Network send rate (default: 12)
- Serialization Rate: State serialization rate (default: 12)
Instantiation
Place vehicle prefab in a Resources folder, then assign it to PhotonManager's OnJoinedInstantiate > Prefabs To Instantiate.
Press Play to test. For multiplayer testing, build the project and run two instances side by side.
How It Works
PhotonMultiplayerVehicle implements IPunObservable to synchronize:
- Input states (steering, throttle, clutch, handbrake, gear shifts)
- Light states
- Engine angular velocity
- Actions (horn, engine start/stop, cruise control, boost, flip over, trailer attach/detach)
The PhotonView owner simulates physics. Remote clients receive and apply the synchronized state.