From 72715278ef341a2b6f946829268f39ad31778d9c Mon Sep 17 00:00:00 2001 From: Fernando Cortez Date: Wed, 20 Sep 2023 15:52:01 -0400 Subject: [PATCH 1/2] listening for NetworkObject parent changes to fix stuck ingredients on disconnects --- .../Assets/Prefabs/Ingredient.prefab | 18 ++++++++++++- .../Scripts/ClientObjectWithIngredientType.cs | 17 +++++++----- .../Assets/Scripts/ClientPlayerMove.cs | 2 +- .../Assets/Scripts/ServerIngredientPhysics.cs | 26 +++++++++++++++++++ .../Scripts/ServerIngredientPhysics.cs.meta | 11 ++++++++ .../Assets/Scripts/ServerPlayerMove.cs | 11 ++------ 6 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 Basic/ClientDriven/Assets/Scripts/ServerIngredientPhysics.cs create mode 100644 Basic/ClientDriven/Assets/Scripts/ServerIngredientPhysics.cs.meta diff --git a/Basic/ClientDriven/Assets/Prefabs/Ingredient.prefab b/Basic/ClientDriven/Assets/Prefabs/Ingredient.prefab index 1d42614b3..d36c425b2 100644 --- a/Basic/ClientDriven/Assets/Prefabs/Ingredient.prefab +++ b/Basic/ClientDriven/Assets/Prefabs/Ingredient.prefab @@ -16,6 +16,7 @@ GameObject: - component: {fileID: 5818429371130516787} - component: {fileID: 5607146804455042385} - component: {fileID: 2549828380439460752} + - component: {fileID: -5120166168328346616} m_Layer: 6 m_Name: Ingredient m_TagString: Untagged @@ -30,6 +31,7 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 8321201880322001125} + serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: -11.03, y: 1.42, z: 7.558644} m_LocalScale: {x: 0.75, y: 0.75, z: 0.75} @@ -41,7 +43,6 @@ Transform: - {fileID: 2558306008476788773} - {fileID: 7478805024049242977} m_Father: {fileID: 0} - m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!135 &4840591773774142929 SphereCollider: @@ -152,6 +153,7 @@ MonoBehaviour: SynchronizeTransform: 1 ActiveSceneSynchronization: 0 SceneMigrationSynchronization: 1 + SpawnWithObservers: 1 DontDestroyWithOwner: 0 AutoObjectParentSync: 1 --- !u!114 &5607146804455042385 @@ -182,6 +184,20 @@ MonoBehaviour: m_BlueMaterial: {fileID: 2100000, guid: b423dced7a4ac4f40a119b84a23cfc9b, type: 2} m_RedMaterial: {fileID: 2100000, guid: d1a9058ddc5c2461298f65541af6fcd9, type: 2} m_ColorMesh: {fileID: 6206319821543937579} +--- !u!114 &-5120166168328346616 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 8321201880322001125} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 18eacd80d71f7c948a562ba85d3618d7, type: 3} + m_Name: + m_EditorClassIdentifier: + m_NetworkTransform: {fileID: 2014424453305718345} + m_Rigidbody: {fileID: 7759258758774188825} --- !u!1001 &2596981318218469326 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Basic/ClientDriven/Assets/Scripts/ClientObjectWithIngredientType.cs b/Basic/ClientDriven/Assets/Scripts/ClientObjectWithIngredientType.cs index 621ac50ad..3e97c7560 100644 --- a/Basic/ClientDriven/Assets/Scripts/ClientObjectWithIngredientType.cs +++ b/Basic/ClientDriven/Assets/Scripts/ClientObjectWithIngredientType.cs @@ -27,11 +27,19 @@ public override void OnNetworkSpawn() { base.OnNetworkSpawn(); enabled = IsClient; + + UpdateMaterial(default(IngredientType), m_Server.currentIngredientType.Value); + m_Server.currentIngredientType.OnValueChanged += UpdateMaterial; } - void UpdateMaterial() + public override void OnNetworkDespawn() { - switch (m_Server.currentIngredientType.Value) + m_Server.currentIngredientType.OnValueChanged -= UpdateMaterial; + } + + void UpdateMaterial(IngredientType previousValue, IngredientType newValue) + { + switch (newValue) { case IngredientType.Blue: m_ColorMesh.material = m_BlueMaterial; @@ -44,10 +52,5 @@ void UpdateMaterial() break; } } - - protected void Update() - { - UpdateMaterial(); // this is not performant to be called every update, don't do this. - } } } \ No newline at end of file diff --git a/Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs b/Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs index e8c9706e6..2c6430f45 100644 --- a/Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs +++ b/Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs @@ -41,7 +41,7 @@ void Awake() // these two components disabled, and will enable a CapsuleCollider. Per the CharacterController documentation: // https://docs.unity3d.com/Manual/CharacterControllers.html, a Character controller can push rigidbody // objects aside while moving but will not be accelerated by incoming collisions. This means that a primitive - // CapusleCollider must instead be used for ghost clients to simulate collisions between owning players and + // CapsuleCollider must instead be used for ghost clients to simulate collisions between owning players and // ghost clients. m_ThirdPersonController.enabled = false; m_CapsuleCollider.enabled = false; diff --git a/Basic/ClientDriven/Assets/Scripts/ServerIngredientPhysics.cs b/Basic/ClientDriven/Assets/Scripts/ServerIngredientPhysics.cs new file mode 100644 index 000000000..866e4ca7d --- /dev/null +++ b/Basic/ClientDriven/Assets/Scripts/ServerIngredientPhysics.cs @@ -0,0 +1,26 @@ +using System; +using Unity.Netcode; +using Unity.Netcode.Components; +using UnityEngine; + +[RequireComponent(typeof(Rigidbody))] +public class ServerIngredientPhysics : NetworkBehaviour +{ + [SerializeField] + NetworkTransform m_NetworkTransform; + + [SerializeField] + Rigidbody m_Rigidbody; + + public override void OnNetworkObjectParentChanged(NetworkObject parentNetworkObject) + { + SetPhysics(parentNetworkObject == null); + } + + void SetPhysics(bool isEnabled) + { + m_Rigidbody.isKinematic = !isEnabled; + m_Rigidbody.interpolation = isEnabled ? RigidbodyInterpolation.Interpolate : RigidbodyInterpolation.None; + m_NetworkTransform.InLocalSpace = !isEnabled; + } +} diff --git a/Basic/ClientDriven/Assets/Scripts/ServerIngredientPhysics.cs.meta b/Basic/ClientDriven/Assets/Scripts/ServerIngredientPhysics.cs.meta new file mode 100644 index 000000000..1cc90ff29 --- /dev/null +++ b/Basic/ClientDriven/Assets/Scripts/ServerIngredientPhysics.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 18eacd80d71f7c948a562ba85d3618d7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs b/Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs index 94d68aba5..d145d24f6 100644 --- a/Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs +++ b/Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs @@ -56,14 +56,10 @@ public void PickupObjectServerRpc(ulong objToPickupID) if (objectToPickup.TryGetComponent(out NetworkObject networkObject) && networkObject.TrySetParent(transform)) { - var pickUpObjectRigidbody = objectToPickup.GetComponent(); - pickUpObjectRigidbody.isKinematic = true; - pickUpObjectRigidbody.interpolation = RigidbodyInterpolation.None; - objectToPickup.GetComponent().InLocalSpace = true; + m_PickedUpObject = networkObject; objectToPickup.transform.localPosition = m_LocalHeldPosition; objectToPickup.GetComponent().ingredientDespawned += IngredientDespawned; isObjectPickedUp.Value = true; - m_PickedUpObject = objectToPickup; } } @@ -78,12 +74,9 @@ public void DropObjectServerRpc() { if (m_PickedUpObject != null) { + m_PickedUpObject.GetComponent().ingredientDespawned -= IngredientDespawned; // can be null if enter drop zone while carrying m_PickedUpObject.transform.parent = null; - var pickedUpObjectRigidbody = m_PickedUpObject.GetComponent(); - pickedUpObjectRigidbody.isKinematic = false; - pickedUpObjectRigidbody.interpolation = RigidbodyInterpolation.Interpolate; - m_PickedUpObject.GetComponent().InLocalSpace = false; m_PickedUpObject = null; } From 509cba06b1b7245bd8e64a27b0abba67eedd9635 Mon Sep 17 00:00:00 2001 From: Fernando Cortez Date: Wed, 20 Sep 2023 16:00:12 -0400 Subject: [PATCH 2/2] changelog addition --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48d721560..e964749c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,9 @@ - Upgraded to Netcode for GameObjects v1.6.0 (#134) - Upgraded sample to 2022.3.9f1 LTS (#134) +#### Fixed +- Added a script to handle NetworkObject parent changes on Ingredients to address a bug where Ingredients would not get stuck on client disconnect events (#136) + ### Dynamic Addressables Network Prefabs #### Changed