Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
611aed6
fix
NoelStephensUnity Aug 17, 2022
e0164c4
fix
NoelStephensUnity Aug 18, 2022
9f5d7ec
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Aug 18, 2022
15fa292
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Aug 24, 2022
413e5ff
style
NoelStephensUnity Aug 24, 2022
05a2e92
style
NoelStephensUnity Aug 24, 2022
e7b1ec6
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Aug 24, 2022
9a92f3a
Update-JOlmer-Suggestions
NoelStephensUnity Aug 24, 2022
f2ad223
Update-JOlmer-Suggestion-2
NoelStephensUnity Aug 24, 2022
2e9b2bb
update
NoelStephensUnity Aug 24, 2022
02b281e
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Aug 24, 2022
4eb3665
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Aug 25, 2022
1b6656e
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 8, 2022
84b18b5
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 8, 2022
9fa5e0b
fix
NoelStephensUnity Sep 9, 2022
80d1af5
test
NoelStephensUnity Sep 9, 2022
c798c47
update
NoelStephensUnity Sep 9, 2022
3704e84
test update
NoelStephensUnity Sep 9, 2022
d24443b
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 9, 2022
eca807d
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 14, 2022
1250f30
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 14, 2022
8d18c1d
update
NoelStephensUnity Sep 14, 2022
80de0e9
fix
NoelStephensUnity Sep 14, 2022
1ef4c63
style
NoelStephensUnity Sep 15, 2022
babd4a2
fix
NoelStephensUnity Sep 15, 2022
d478169
test
NoelStephensUnity Sep 15, 2022
ae8fade
update
NoelStephensUnity Sep 15, 2022
5016303
update
NoelStephensUnity Sep 15, 2022
2e43737
style
NoelStephensUnity Sep 15, 2022
95ae7bb
fix
NoelStephensUnity Sep 15, 2022
c735a38
update
NoelStephensUnity Sep 15, 2022
2d98f75
refactor
NoelStephensUnity Sep 15, 2022
76c0e85
fix
NoelStephensUnity Sep 16, 2022
d7384b4
style
NoelStephensUnity Sep 16, 2022
e767757
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 19, 2022
505b27b
update
NoelStephensUnity Sep 19, 2022
0408358
update
NoelStephensUnity Sep 20, 2022
839ff2e
update
NoelStephensUnity Sep 20, 2022
0b7b3ae
update
NoelStephensUnity Sep 20, 2022
8ea66a8
update
NoelStephensUnity Sep 21, 2022
2488249
style
NoelStephensUnity Sep 21, 2022
19a291d
update
NoelStephensUnity Sep 21, 2022
0532a4f
update
NoelStephensUnity Sep 21, 2022
7817eb1
fix
NoelStephensUnity Sep 23, 2022
02de172
test updates
NoelStephensUnity Sep 23, 2022
1e19f30
update
NoelStephensUnity Sep 23, 2022
98318cf
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 23, 2022
fdbad87
style
NoelStephensUnity Sep 23, 2022
0a1558f
test update
NoelStephensUnity Sep 23, 2022
b2e4516
update
NoelStephensUnity Sep 23, 2022
a7b1e72
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 23, 2022
aab4980
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 23, 2022
aecb750
update
NoelStephensUnity Sep 23, 2022
46eb1ef
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 23, 2022
0a74e63
test update
NoelStephensUnity Sep 23, 2022
1a87cdb
fix
NoelStephensUnity Sep 23, 2022
c659916
style
NoelStephensUnity Sep 23, 2022
4a60e1b
test
NoelStephensUnity Sep 23, 2022
0a3f0d8
style
NoelStephensUnity Sep 23, 2022
1972488
fix
NoelStephensUnity Sep 27, 2022
289bf72
test in-scene placed
NoelStephensUnity Sep 27, 2022
a51ae69
update
NoelStephensUnity Sep 27, 2022
719ffa2
update and style
NoelStephensUnity Sep 27, 2022
1ab8128
test
NoelStephensUnity Sep 27, 2022
30ba251
update
NoelStephensUnity Sep 27, 2022
0adc908
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 27, 2022
b9fb55b
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 27, 2022
45e8c8d
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 28, 2022
da1372b
style
NoelStephensUnity Sep 28, 2022
682c020
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 28, 2022
058f401
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 28, 2022
dc54216
style
NoelStephensUnity Sep 28, 2022
89a50a4
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Sep 30, 2022
7e5bcd3
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Oct 1, 2022
c13b3f2
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Oct 3, 2022
59cf57a
Merge branch 'develop' into fix/parenting-does-not-preserve-worldposi…
NoelStephensUnity Oct 3, 2022
e6d654c
style
NoelStephensUnity Oct 3, 2022
4db2627
update
NoelStephensUnity Oct 4, 2022
9ab7199
Merge develop into fix/parenting-does-not-preserve-worldpositionstays…
netcode-ci-service Oct 4, 2022
956d331
Merge develop into fix/parenting-does-not-preserve-worldpositionstays…
netcode-ci-service Oct 4, 2022
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
29 changes: 20 additions & 9 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ internal void InvokeBehaviourOnNetworkObjectParentChanged(NetworkObject parentNe
private bool m_IsReparented; // Did initial parent (came from the scene hierarchy) change at runtime?
private ulong? m_LatestParent; // What is our last set parent NetworkObject's ID?
private Transform m_CachedParent; // What is our last set parent Transform reference?
private bool m_ChildWorldPositionStays = true; // Used to preserve the world position stays parameter passed in TrySetParent

internal void SetCachedParent(Transform parentTransform)
{
Expand All @@ -583,10 +584,11 @@ internal void SetCachedParent(Transform parentTransform)

internal (bool IsReparented, ulong? LatestParent) GetNetworkParenting() => (m_IsReparented, m_LatestParent);

internal void SetNetworkParenting(bool isReparented, ulong? latestParent)
internal void SetNetworkParenting(bool isReparented, ulong? latestParent, bool childWorldPositionStays)
Comment thread
NoelStephensUnity marked this conversation as resolved.
Outdated
{
m_IsReparented = isReparented;
m_LatestParent = latestParent;
m_ChildWorldPositionStays = childWorldPositionStays;
}

/// <summary>
Expand Down Expand Up @@ -648,7 +650,7 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
{
return false;
}

m_ChildWorldPositionStays = worldPositionStays;
transform.SetParent(parent.transform, worldPositionStays);
return true;
}
Expand Down Expand Up @@ -709,6 +711,7 @@ private void OnTransformParentChanged()
else
{
m_LatestParent = null;
m_ChildWorldPositionStays = true; // Reset to the default setting of true
}

m_IsReparented = true;
Expand Down Expand Up @@ -770,6 +773,7 @@ internal bool ApplyNetworkParenting()
{
m_CachedParent = null;
transform.parent = null;
m_ChildWorldPositionStays = true; // Reset to the default setting of true

InvokeBehaviourOnNetworkObjectParentChanged(null);
return true;
Expand All @@ -784,7 +788,7 @@ internal bool ApplyNetworkParenting()
var parentObject = NetworkManager.SpawnManager.SpawnedObjects[m_LatestParent.Value];

m_CachedParent = parentObject.transform;
transform.parent = parentObject.transform;
transform.SetParent(parentObject.transform, m_ChildWorldPositionStays);

InvokeBehaviourOnNetworkObjectParentChanged(parentObject);
return true;
Expand Down Expand Up @@ -997,12 +1001,14 @@ public struct TransformData : INetworkSerializeByMemcpy

public int NetworkSceneHandle;

public bool ChildWorldPositionStays;

public unsafe void Serialize(FastBufferWriter writer)
{
var writeSize = sizeof(HeaderData);
writeSize += Header.HasParent ? FastBufferWriter.GetWriteSize(ParentObjectId) : 0;
writeSize += Header.HasTransform ? FastBufferWriter.GetWriteSize(Transform) : 0;
writeSize += Header.IsReparented ? FastBufferWriter.GetWriteSize(IsLatestParentSet) + (IsLatestParentSet ? FastBufferWriter.GetWriteSize<ulong>() : 0) : 0;
writeSize += Header.IsReparented ? FastBufferWriter.GetWriteSize(IsLatestParentSet) + FastBufferWriter.GetWriteSize<bool>() + (IsLatestParentSet ? FastBufferWriter.GetWriteSize<ulong>() : 0) : 0;
Comment thread
NoelStephensUnity marked this conversation as resolved.
Outdated
writeSize += Header.IsSceneObject ? FastBufferWriter.GetWriteSize<int>() : 0;

if (!writer.TryBeginWrite(writeSize))
Expand All @@ -1025,6 +1031,7 @@ public unsafe void Serialize(FastBufferWriter writer)
if (Header.IsReparented)
{
writer.WriteValue(IsLatestParentSet);
writer.WriteValue(ChildWorldPositionStays);
if (IsLatestParentSet)
{
writer.WriteValue((ulong)LatestParent);
Expand Down Expand Up @@ -1053,7 +1060,7 @@ public unsafe void Deserialize(FastBufferReader reader)
reader.ReadValue(out Header);
var readSize = Header.HasParent ? FastBufferWriter.GetWriteSize(ParentObjectId) : 0;
readSize += Header.HasTransform ? FastBufferWriter.GetWriteSize(Transform) : 0;
readSize += Header.IsReparented ? FastBufferWriter.GetWriteSize(IsLatestParentSet) + (IsLatestParentSet ? FastBufferWriter.GetWriteSize<ulong>() : 0) : 0;
readSize += Header.IsReparented ? FastBufferWriter.GetWriteSize(IsLatestParentSet) + FastBufferWriter.GetWriteSize<bool>() + (IsLatestParentSet ? FastBufferWriter.GetWriteSize<ulong>() : 0) : 0;
Comment thread
NoelStephensUnity marked this conversation as resolved.
Outdated
readSize += Header.IsSceneObject ? FastBufferWriter.GetWriteSize<int>() : 0;

if (!reader.TryBeginRead(readSize))
Expand All @@ -1074,6 +1081,7 @@ public unsafe void Deserialize(FastBufferReader reader)
if (Header.IsReparented)
{
reader.ReadValue(out IsLatestParentSet);
reader.ReadValue(out ChildWorldPositionStays);
Comment thread
NoelStephensUnity marked this conversation as resolved.
Outdated
if (IsLatestParentSet)
{
reader.ReadValueSafe(out ulong latestParent);
Expand Down Expand Up @@ -1126,8 +1134,10 @@ internal SceneObject GetMessageSceneObject(ulong targetClientId)
obj.Header.HasTransform = true;
obj.Transform = new SceneObject.TransformData
{
Position = transform.position,
Rotation = transform.rotation
// If we are parented and we have the m_ChildWorldPositionStays disabled, then use the default
// values as opposed to the world space values and adjust relative to the parent.
Position = parentNetworkObject && !m_ChildWorldPositionStays ? Vector3.zero : transform.position,
Rotation = parentNetworkObject && !m_ChildWorldPositionStays ? Quaternion.identity : transform.rotation
};
}

Expand All @@ -1137,6 +1147,7 @@ internal SceneObject GetMessageSceneObject(ulong targetClientId)
{
var isLatestParentSet = latestParent != null && latestParent.HasValue;
obj.IsLatestParentSet = isLatestParentSet;
obj.ChildWorldPositionStays = m_ChildWorldPositionStays;
if (isLatestParentSet)
{
obj.LatestParent = latestParent.Value;
Expand Down Expand Up @@ -1182,7 +1193,7 @@ internal static NetworkObject AddSceneObject(in SceneObject sceneObject, FastBuf
sceneObject.Header.IsSceneObject, sceneObject.Header.Hash,
sceneObject.Header.OwnerClientId, parentNetworkId, networkSceneHandle, position, rotation, sceneObject.Header.IsReparented);

networkObject?.SetNetworkParenting(sceneObject.Header.IsReparented, sceneObject.LatestParent);
networkObject?.SetNetworkParenting(sceneObject.Header.IsReparented, sceneObject.LatestParent, sceneObject.ChildWorldPositionStays);

if (networkObject == null)
{
Expand All @@ -1197,7 +1208,7 @@ internal static NetworkObject AddSceneObject(in SceneObject sceneObject, FastBuf
return null;
}

// Spawn the NetworkObject(
// Spawn the NetworkObject
networkManager.SpawnManager.SpawnNetworkObjectLocally(networkObject, sceneObject, variableData, false);

return networkObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ internal struct ParentSyncMessage : INetworkMessage

public bool IsReparented;

public bool ChildWorldPositionStays;

//If(Metadata.IsReparented)
public bool IsLatestParentSet;

//If(IsLatestParentSet)
public ulong? LatestParent;



public void Serialize(FastBufferWriter writer)
{
writer.WriteValueSafe(NetworkObjectId);
writer.WriteValueSafe(IsReparented);
writer.WriteValueSafe(ChildWorldPositionStays);
if (IsReparented)
{
writer.WriteValueSafe(IsLatestParentSet);
Expand All @@ -36,6 +41,7 @@ public bool Deserialize(FastBufferReader reader, ref NetworkContext context)

reader.ReadValueSafe(out NetworkObjectId);
reader.ReadValueSafe(out IsReparented);
reader.ReadValueSafe(out ChildWorldPositionStays);
if (IsReparented)
{
reader.ReadValueSafe(out IsLatestParentSet);
Expand All @@ -59,7 +65,7 @@ public void Handle(ref NetworkContext context)
{
var networkManager = (NetworkManager)context.SystemOwner;
var networkObject = networkManager.SpawnManager.SpawnedObjects[NetworkObjectId];
networkObject.SetNetworkParenting(IsReparented, LatestParent);
networkObject.SetNetworkParenting(IsReparented, LatestParent, ChildWorldPositionStays);
networkObject.ApplyNetworkParenting();
}
}
Expand Down