Skip to content

Commit 2adab56

Browse files
fix: imps self-destroying on clients when late-joining with multiple additive scenes loaded (#574)
* Disabled ClientCharacterVisualization before OnNetworkSpawn * Removed Parent property from ClientCharacterVisualization and added public property to get NetState
1 parent ad44e93 commit 2adab56

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

Assets/BossRoom/Scripts/Client/Game/Action/FXProjectileTargetedActionFX.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ private NetworkObject GetTarget()
120120
/// </summary>
121121
private bool IsParentAnNPC()
122122
{
123-
var targetable = m_Parent.Parent.GetComponent<ITargetable>();
124-
return targetable.IsNpc;
123+
return m_Parent.NetState.IsNpc;
125124
}
126125

127126

Assets/BossRoom/Scripts/Client/Game/Action/TargetActionFX.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public TargetActionFX(ref ActionRequestData data, ClientCharacterVisualization p
2222
public override bool Start()
2323
{
2424
base.Start();
25-
m_ParentState = m_Parent.Parent.GetComponent<NetworkCharacterState>();
25+
m_ParentState = m_Parent.NetState;
2626

2727
m_ParentState.TargetId.OnValueChanged += OnTargetChanged;
2828
m_ParentState.GetComponent<Client.ClientInputSender>().ActionInputEvent += OnActionInput;

Assets/BossRoom/Scripts/Client/Game/Character/ClientCharacterVisualization.cs

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,14 @@ public class ClientCharacterVisualization : NetworkBehaviour
3939
/// </summary>
4040
public Material ReticuleFriendlyMat => m_VisualizationConfiguration.ReticuleFriendlyMat;
4141

42-
/// <summary>
43-
/// Returns our pseudo-Parent, the object that owns the visualization.
44-
/// (We don't have an actual transform parent because we're on a top-level GameObject.)
45-
/// </summary>
46-
public Transform Parent { get; private set; }
47-
4842
PhysicsWrapper m_PhysicsWrapper;
4943

5044
public bool CanPerformActions => m_NetState.CanPerformActions;
5145

5246
NetworkCharacterState m_NetState;
5347

48+
public NetworkCharacterState NetState => m_NetState;
49+
5450
ActionVisualization m_ActionViz;
5551

5652
PositionLerper m_PositionLerper;
@@ -64,20 +60,24 @@ public class ClientCharacterVisualization : NetworkBehaviour
6460

6561
Quaternion m_LerpedRotation;
6662

63+
void Awake()
64+
{
65+
enabled = false;
66+
}
67+
6768
public override void OnNetworkSpawn()
6869
{
6970
if (!IsClient || transform.parent == null)
7071
{
71-
enabled = false;
7272
return;
7373
}
7474

75+
enabled = true;
76+
7577
m_ActionViz = new ActionVisualization(this);
7678

7779
m_NetState = GetComponentInParent<NetworkCharacterState>();
7880

79-
Parent = m_NetState.transform;
80-
8181
m_PhysicsWrapper = m_NetState.GetComponent<PhysicsWrapper>();
8282

8383
m_NetState.DoActionEventClient += PerformActionFX;
@@ -99,7 +99,7 @@ public override void OnNetworkSpawn()
9999
{
100100
name = "AvatarGraphics" + m_NetState.OwnerClientId;
101101

102-
if (Parent.TryGetComponent(out ClientAvatarGuidHandler clientAvatarGuidHandler))
102+
if (m_NetState.TryGetComponent(out ClientAvatarGuidHandler clientAvatarGuidHandler))
103103
{
104104
m_ClientVisualsAnimator = clientAvatarGuidHandler.graphicsAnimator;
105105
}
@@ -115,7 +115,7 @@ public override void OnNetworkSpawn()
115115
m_ActionViz.PlayAction(ref data);
116116
gameObject.AddComponent<CameraController>();
117117

118-
if (Parent.TryGetComponent(out ClientInputSender inputSender))
118+
if (m_NetState.TryGetComponent(out ClientInputSender inputSender))
119119
{
120120
// TODO: revisit; anticipated actions would play twice on the host
121121
if (!IsServer)
@@ -128,22 +128,8 @@ public override void OnNetworkSpawn()
128128
}
129129
}
130130

131-
void OnActionInput(ActionRequestData data)
132-
{
133-
m_ActionViz.AnticipateAction(ref data);
134-
}
135-
136-
void OnMoveInput(Vector3 position)
137-
{
138-
if (!IsAnimating())
139-
{
140-
OurAnimator.SetTrigger(m_VisualizationConfiguration.AnticipateMoveTriggerID);
141-
}
142-
}
143-
144-
public override void OnDestroy()
131+
public override void OnNetworkDespawn()
145132
{
146-
base.OnDestroy();
147133
if (m_NetState)
148134
{
149135
m_NetState.DoActionEventClient -= PerformActionFX;
@@ -152,12 +138,27 @@ public override void OnDestroy()
152138
m_NetState.OnStopChargingUpClient -= OnStoppedChargingUp;
153139
m_NetState.IsStealthy.OnValueChanged -= OnStealthyChanged;
154140

155-
if (Parent != null && Parent.TryGetComponent(out ClientInputSender sender))
141+
if (m_NetState.TryGetComponent(out ClientInputSender sender))
156142
{
157143
sender.ActionInputEvent -= OnActionInput;
158144
sender.ClientMoveEvent -= OnMoveInput;
159145
}
160146
}
147+
148+
enabled = false;
149+
}
150+
151+
void OnActionInput(ActionRequestData data)
152+
{
153+
m_ActionViz.AnticipateAction(ref data);
154+
}
155+
156+
void OnMoveInput(Vector3 position)
157+
{
158+
if (!IsAnimating())
159+
{
160+
OurAnimator.SetTrigger(m_VisualizationConfiguration.AnticipateMoveTriggerID);
161+
}
161162
}
162163

163164
void PerformActionFX(ActionRequestData data)
@@ -238,13 +239,6 @@ float GetVisualMovementSpeed()
238239

239240
void Update()
240241
{
241-
if (Parent == null)
242-
{
243-
// since we aren't in the transform hierarchy, we have to explicitly die when our parent dies.
244-
Destroy(gameObject);
245-
return;
246-
}
247-
248242
// On the host, Characters are translated via ServerCharacterMovement's FixedUpdate method. To ensure that
249243
// the game camera tracks a GameObject moving in the Update loop and therefore eliminate any camera jitter,
250244
// this graphics GameObject's position is smoothed over time on the host. Clients do not need to perform any

0 commit comments

Comments
 (0)