Skip to content
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
18 changes: 17 additions & 1 deletion Basic/ClientDriven/Assets/Prefabs/Ingredient.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand All @@ -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:
Expand Down Expand Up @@ -152,6 +153,7 @@ MonoBehaviour:
SynchronizeTransform: 1
ActiveSceneSynchronization: 0
SceneMigrationSynchronization: 1
SpawnWithObservers: 1
DontDestroyWithOwner: 0
AutoObjectParentSync: 1
--- !u!114 &5607146804455042385
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,10 +52,5 @@ void UpdateMaterial()
break;
}
}

protected void Update()
{
UpdateMaterial(); // this is not performant to be called every update, don't do this.
}
}
}
2 changes: 1 addition & 1 deletion Basic/ClientDriven/Assets/Scripts/ClientPlayerMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 26 additions & 0 deletions Basic/ClientDriven/Assets/Scripts/ServerIngredientPhysics.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 2 additions & 9 deletions Basic/ClientDriven/Assets/Scripts/ServerPlayerMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,10 @@ public void PickupObjectServerRpc(ulong objToPickupID)

if (objectToPickup.TryGetComponent(out NetworkObject networkObject) && networkObject.TrySetParent(transform))
{
var pickUpObjectRigidbody = objectToPickup.GetComponent<Rigidbody>();
pickUpObjectRigidbody.isKinematic = true;
pickUpObjectRigidbody.interpolation = RigidbodyInterpolation.None;
objectToPickup.GetComponent<NetworkTransform>().InLocalSpace = true;
m_PickedUpObject = networkObject;
objectToPickup.transform.localPosition = m_LocalHeldPosition;
objectToPickup.GetComponent<ServerIngredient>().ingredientDespawned += IngredientDespawned;
isObjectPickedUp.Value = true;
m_PickedUpObject = objectToPickup;
}
}

Expand All @@ -78,12 +74,9 @@ public void DropObjectServerRpc()
{
if (m_PickedUpObject != null)
{
m_PickedUpObject.GetComponent<ServerIngredient>().ingredientDespawned -= IngredientDespawned;
// can be null if enter drop zone while carrying
m_PickedUpObject.transform.parent = null;
var pickedUpObjectRigidbody = m_PickedUpObject.GetComponent<Rigidbody>();
pickedUpObjectRigidbody.isKinematic = false;
pickedUpObjectRigidbody.interpolation = RigidbodyInterpolation.Interpolate;
m_PickedUpObject.GetComponent<NetworkTransform>().InLocalSpace = false;
m_PickedUpObject = null;
}

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down