Skip to content

Add Boss Room educational content #827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/learn/bossroom/networkobject-parenting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
id: networkobject-parenting
title: NetworkObject Parenting inside Boss Room
description: Learn about Boss Room's approach to NetworkObject parenting.
---
:::note
Required reading: [NetworkObject Parenting](../../advanced-topics/networkobject-parenting.md)
:::

Before detailing Boss Room’s approach toon `NetworkObject` parenting, it’s important to highlight a limitation of Netcode: A dynamically-spawned `NetworkObject` **cannot** contain another `NetworkObject` in its hierarchy. If you spawn such a `NetworkObject`, you cannot spawn children `NetworkObjects`. You can only add children `NetworkObject` components to a `NetworkObject` that is part of a scene.

Boss Room leverages `NetworkObject` parenting through the server-driven `PickUp` action (see [`PickUpAction.cs`](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Assets/Scripts/Gameplay/Action/ConcreteActions/PickUpAction.cs)), where a character has the ability to pick up a specially-tagged, in-scene placed `NetworkObject` (see [`PickUpPot` prefab](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Assets/Prefabs/Game/PickUpPot.prefab)].

At its root, `PickUpPot` contains a `NetworkObject`, a `NetworkTransform`, and a `PositionConstraint` component. `AutoObjectParentSync` is enabled on its `NetworkTransform` (as is by default) so that:

1. The `NetworkObject` can verify server-side if parenting a Heavy object to another `NetworkObject` is allowed.
2. The `NetworkObject` can notify us when the parent has successfully been modified server-side.

To accommodate the limitation highlighted at the beginning of this document, Boss Room leverages the `PositionConstraint` component to simulate an object following a character’s position.

A special hand bone has been added to our Character’s avatar. Upon a character’s successful parenting attempt, this special bone is set as the `PickUpPot`’s `PositonConstraint` target. So while the `PickUpPot` is technically parented to a player, the `PositionConstraint` component allows the `PickUpPot` to follow a bone’s position, presenting the **illusion** that the `PickUpPot` is parented to the player’s hand bone itself.

Once the `PickUpPot` is parented, local space simulation is enabled on its [`NetworkTransform` component](../../components/networktransform.md).
12 changes: 12 additions & 0 deletions docs/learn/bossroom/networkrigidbody.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
id: networkrigidbody
title: NetworkRigidbody inside Boss Room
description: Learn how Boss Room leverages NetworkRigidbody.
---
:::note
Required reading: [Physics](../..//advanced-topics/physics.md)
:::

Boss Room leverages `NetworkRigidbody` to simulate physics-based projectiles. See the Vandal Imp’s tossed projectile, the [`ImpTossedItem` prefab](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Assets/Prefabs/Game/ImpTossedItem.prefab). At its root, this prefab contains a `NetworkObject`, a `NetworkTransform`, a `Rigidbody`, and a `NetworkRigidbody` component. Refer to [`TossAction.cs`](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Assets/Scripts/Gameplay/Action/ConcreteActions/TossAction.cs) for more implementation details.

An important note: Any modifications to a `Rigidbody` that involve Physics (that is, modifying velocity, applying forces, applying torque, and the like) must be done **after** the `NetworkObject` is spawned since `NetworkRigidbody` forces the Rigidbody’s `isKinematic` flag to be true on `Awake()`. Once spawned, this flag is modified depending on the ownership status of the `NetworkObject`.
27 changes: 27 additions & 0 deletions docs/learn/bossroom/spawn-networkobjects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
id: spawn-networkobjects
title: Dynamically spawning NetworkObjects in Boss Room to Resolve Zombie NetworkObjects
description: Learn how to dynamically spawn NetworkObjects in Boss Room to resolve zombie NetworkObjects.
---
:::note
Required reading: [Object Spawning](../../basics/object-spawning.md).
:::

This document serves as a walkthrough of Boss Room’s approach to solving the following issue: Late-joining clients entering networked sessions encountering zombie `NetworkObjects`. Zombie `NetworkObjects` represent `NetworkObjects` that are instantiated for a client due to scene loads but are not despawned or destroyed by Netcode.

This is a particular Netcode limitation of `NetworkObject` spawning: `NetworkObjects` that belong to a scene object should not be destroyed until its scene has been unloaded through Netcode’s scene management.

The scenario in question:

* A host loads a scene and destroys a `NetworkObject` that belonged to that scene.
* A client joins the host’s session and loads the additive scene. This operation loads **all** the `GameObjects` included in the additive scene. The `NetworkObject` that was destroyed on the server will not be destroyed on the client’s machine.

This scenario manifested inside Boss Room, whereby late-joining clients joining a game session encountered zombie `NetworkObject`s that were not destroyed over the network.

Additive scenes now contain prefab instances of a custom spawner, [`NetworkObjectSpawner`](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/NetworkObjectSpawner.cs) to accommodate this visual inconsistency.

Compositionally, these additive scenes now only contain the following:

* Prefab instances of level geometry.
* Prefab instances of `NetworkObject`s that will **not** be despawned nor destroyed for the scene’s lifetime. Examples include [BreakablePot](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Assets/Prefabs/Game/BreakablePot.prefab) and [BreakableCrystal](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Assets/Prefabs/Game/BreakableCrystal.prefab).
* Prefab instances of `NetworkObjectSpawner`, which spawn `NetworkObject`s that **may** be destroyed or despawned during the scene’s lifetime. Examples include [Imp](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Assets/GameData/Character/Imp/Imp.asset), [VandalImp](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Assets/GameData/Character/VandalImp/VandalImp.asset), and [ImpBoss](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Assets/GameData/Character/ImpBoss/ImpBoss.asset).
12 changes: 12 additions & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,18 @@ module.exports = {
"type": "doc",
"id": "learn/bossroom/bossroom-actions"
},
{
"type": "doc",
"id": "learn/bossroom/networkobject-parenting"
},
{
"type": "doc",
"id": "learn/bossroom/networkrigidbody"
},
{
"type": "doc",
"id": "learn/bossroom/spawn-networkobjects"
},
],
},
{
Expand Down