CharacterHealth/Health improvement when using managing Characters with the ObjectPool

echtnice

Member
Hi,

i am using the ObjectPool to reuse UCC Characters. Spawned characters are put back into the ObjectPool when they are to far away from the player. My characters do not "Die" but I do use "CharacterHealth.Respawn" to reset the character after reusing it from the pool.

After the respawn, the Layer of my characters got initialised to (Default / 0) because CharacterHealth / Health have a Death layer configured and the Respawn tries to reset them to the Layers that where active the moment the character died (which mine did not).

For now Iremember the layers not only when the character dies, but also in Awake. This ensures, the character has the correct layer again.

Thanks

Code:
diff --git a/Assets/Opsive/UltimateCharacterController/Scripts/Traits/CharacterHealth.cs b/Assets/Opsive/UltimateCharacterController/Scripts/Traits/CharacterHealth.cs
index a037c0f7..f5550b58 100644
--- a/Assets/Opsive/UltimateCharacterController/Scripts/Traits/CharacterHealth.cs
+++ b/Assets/Opsive/UltimateCharacterController/Scripts/Traits/CharacterHealth.cs
@@ -71,7 +71,10 @@ private void Start()
                 m_ColliderGameObjects[i] = m_CharacterLocomotion.Colliders[i].gameObject;
             }
             m_ColliderLayers = new int[m_CharacterLocomotion.ColliderCount];
-
+            //GUNFU: 18.06.2020 don't change the layer to default (0) if character did not die.
+            for (int i = 0; i < m_ColliderGameObjects.Length; ++i) {
+                m_ColliderLayers[i] = m_ColliderGameObjects[i].layer;
+            }
             if (!string.IsNullOrEmpty(m_DamagedEffectName)) {
                 m_DamagedEffect = m_CharacterLocomotion.GetEffect(UnityEngineUtility.GetType(m_DamagedEffectName), m_DamagedEffectIndex);
             }
diff --git a/Assets/Opsive/UltimateCharacterController/Scripts/Traits/Health.cs b/Assets/Opsive/UltimateCharacterController/Scripts/Traits/Health.cs
index ecf0c329..fd3fd252 100644
--- a/Assets/Opsive/UltimateCharacterController/Scripts/Traits/Health.cs
+++ b/Assets/Opsive/UltimateCharacterController/Scripts/Traits/Health.cs
@@ -147,6 +147,8 @@ protected override void Awake()
             if (!string.IsNullOrEmpty(m_ShieldAttributeName)) {
                 m_ShieldAttribute = m_AttributeManager.GetAttribute(m_ShieldAttributeName);
             }
+            //GUNFU: 18.06.2020 don't change the layer to default (0) if character did not die.
+            m_AliveLayer = m_GameObject.layer;
 #if ULTIMATE_CHARACTER_CONTROLLER_MULTIPLAYER
             m_NetworkInfo = m_GameObject.GetCachedComponent<INetworkInfo>();
             m_NetworkHealthMonitor = m_GameObject.GetCachedComponent<INetworkHealthMonitor>();
 
Top