Multiplayer Quickstart
Tutorial: Physics Cube Throwing
This basic tutorial will show you how to spawn colored cubes from the local player and throw them around the scene.
Step 1: Setup and create a cube prefab
- Create a new Space Package
- Create the cube prefab:
- Create a Cube in the scene with
GameObject > 3D Object > Cube
and add aRigidbody
component to it. - Set the local scale to
(0.3, 0.3, 0.3)
so it's not too big. - Add a
Spatial Network Object
component to the GameObject, and enablePosition
,Rotation
andRigidbody
sync. - Save it as a prefab anywhere in your project (drag and drop the GameObject into the Assets folder).
- Create a Cube in the scene with
- Hit
Test Active Scene
to test the space in the Spatial Sandbox
The cube prefab is embedded in the scene and is already spawned when you load the space. You should see the cube fall to the ground.
Step 2: Spawning cubes from the local player
- Now, lets remove the cube from the scene and spawn it dynamically based on input.
- Add a C# assembly to the space in the
Space Package Config
- Create a new script (e.g.
CubeSpawner.cs
) in that assembly and attach it to a GameObject in the scene. We will use this to spawn cubes based on keyboard input. - Assign the cube we created earlier to the
cubePrefab
field in the inspector. - We will also need to register this network prefab in our
Space Package Config
. Add an element to "Network Prefabs" and drag the cube prefab into the field. - Hit
Test Active Scene
to test if the cube spawns when you pressE
.
Step 3: Add some color variation
Now lets give each cube a random color.
- Create a new
Network Behaviour
script (e.g.ColoredCube.cs
) and attach it to the cube prefab. - Test the space again and you should see cubes of different colors being spawned when you press
E
.
Step 4: Cleanup
If we keep spawning cubes, the scene will get cluttered and we start to see performance issues. Let's clean up the cubes after a certain time.
- First lets enable the
DestroyWhenCreatorLeaves
object flag on the Network Object component of the cube prefab. This covers the case for cleaning up the object in case of unexpected disconnections. - Add logic to the
ColoredCube
behaviour to destroy the cube after a few seconds. - Test the space again and you should see the cubes disappear after a few seconds.
Templates
Pre-built examples to help you get started with multiplayer development.
- Matchmaking Demo - Shows how to create a simple matchmaking system.